excel vba - Selenium 查找元素

问题描述

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

名称或 ID 不是一个选项时,我总是很难找到元素。

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

我正在尝试找到登录按钮。 元素看起来像:

enter image description here

我试过了

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

FindElementsByLinkText("Log In",10000)

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

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

请问有什么帮助吗? (如果您能解释如何解决这个问题,那么我将来不会遇到真正值得赞赏的问题)

解决方法

我测试过。试试这个代码下面的代码。它对我有用..

子登录代码()

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(Sec As Integer)

将时间变长

lngTime = Timer
While Timer < lngTime + Sec
    DoEvents
Wend

结束子

,

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

  • 使用FindElementByLinkText

    FindElementByLinkText("Log In")
    
  • 使用FindElementByCss

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

    FindElementByXPath("//a[contains(@href,'goToLoginPage')]")