1

我正在使用 Selenium 进行一些网页抓取。

当名称或 ID 不是选项时,我总是难以找到元素。

该页面是 https://www.portfolio123.com/holdings.jsp?portid=1637063

我正在尝试找到登录按钮。元素看起来像: 在此处输入图像描述

我试过了

FindElementByClass("btn-primary btn-sm", 10000)

FindElementsByLinkText("Log In", 10000)

FindElementByXPath("//div[@class='btn btn-primary btn-sm']//[@href='javascript:void(goToLoginPage())")

但没有任何成功(我怀疑我需要使用 XPath,但我似乎无法正确

请问有什么帮助吗?(如果你能解释如何解决这个问题,那么我以后就不会遇到真正感激的问题了)

4

2 回答 2

1

我测试过。试试下面的代码。它为我工作..

子登录代码()

Dim bot 作为新的 WebDriver

bot.start "chrome", "https://www.portfolio123.com/holdings.jsp?portid=1637063"

bot.Get "/"

调用 WaitForSec(5)

bot.FindElementByXPath("//*[@id=""wrapper""]/div/div/div/a[1]").点击

结束子

Sub WaitForSec(秒为整数)

调光时间只要

lngTime = Timer
While Timer < lngTime + Sec
    DoEvents
Wend

结束子

于 2021-01-23T11:38:22.050 回答
1

要定位元素Log In,您可以使用以下任一Locator Strategies

  • 使用FindElementByLinkText

    FindElementByLinkText("Log In")
    
  • 使用FindElementByCss

    FindElementByCss("a[href*='goToLoginPage']")
    
  • 使用FindElementByXPath

    FindElementByXPath("//a[contains(@href, 'goToLoginPage')]")
    
于 2021-01-23T11:54:48.600 回答