我在selenium vba中打开了一个页面,在这个页面中,我有很多具有相同类名称的元素,当我用代码获得这个元素时: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
看起来你想要特定的元素:
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
注意,你也可以使用item和counter来循环。