如何使用selenium v​​ba转到具有相同类名的页面中的下一个元素

时间:2016-10-28 19:16:13

标签: excel vba selenium

我在selenium v​​ba中打开了一个页面,在这个页面中我有很多具有相同类名的元素,当我得到这个带有代码的元素时:People = driver.findElementByClass("labelinfo").Text它返回给我第一个元素在页面中,我想要元素编号7,所以,我使用代码:driver.findElementsByClassName("labelinfo").Count,它说页面有27个元素“labelinfo”。那么,我如何使用for循环逐个元素地“逐行”直到它到达7号元素? 我的代码是:

    Public Sub AbreELogaNoForum()
    Dim i As Integer, MyPass As String, MyLogin As String, nome As String, data As String, carteira As String, guia As String, test As Long

    Application.ScreenUpdating = False
redo:
    MyLogin = Application.InputBox("Por Favor entre com o Login")
    MyPass = Application.InputBox("Por favor entre com a senha")
    If MyLogin = "" Or MyPass = "" Then GoTo redo
    driver.Start "chrome", "http://rda.unimednc.com.br/"
    driver.setImplicitWait 50000

    driver.Open "http://rda.unimednc.com.br/"
    driver.findElementById("login").SendKeys MyLogin
    driver.findElementById("password").SendKeys MyPass
    driver.findElementById("Button_DoLogin").Click

    test = 7
    Range("B1").Select
    For i = 1 To 10
    MsgBox ("Esperando")
    'nome = driver.findElementByClass("labelinfo").Text
    ActiveCell.Value = driver.findElementsByClassName("labelinfo").Count  
    ActiveCell.Offset(1, 0).Select
    Next

End Sub

Public Sub FechaBrowser()
    driver.stop
End Sub

1 个答案:

答案 0 :(得分:0)

看起来你想要特定的元素:

Dim oElementLooked as WebElement
set oElementLooked = driver.findElementsByClassName("labelinfo").Item(7)

如果您想循环回复,可以执行以下操作:

Dim colNodes as WebElements
Dim oElementLooked as WebElement
set colNodes = driver.findElementsByClassName("labelinfo")
for each oElementLooked in colNodes
     ' Your code here
Next oElementLooked

请注意,您还可以使用项目和计数器来循环。