Selenium Web驱动程序VBA-错误的新循环



晚安

如果可能的话,我想要一些帮助。我正在把这些代码放在一起,但如果它找不到页面项,我就无法创建输出。我希望如果他找不到整数,他会转到下一行,不要显示错误消息,因为我有很多数据要捕获。


Dim drive As New Selenium.ChromeDriver
Dim ks As Selenium.Keys
Dim tempo As Integer
Set ks = New Selenium.Keys
site = "http://www.google.com"

Plan1.Select

tell = Range("A" & Rows.Count).End(xlUp).Row
Line = 4

drive.Start
drive.Get site
Do Until Cells(Linha, 1) = ""

For Line = 4 To tell
Number = Range("A" & Linha).Value

drive.SwitchToFrame drive.FindElementById("WIDGET_ID_4")
Set telephone = drive.FindElementByXPath("/html/body/.........")
Range("B" & Line).Value = telephone.Attribute("outerText")

drive.SwitchToDefaultContent



Next Linha

Loop

drive.Quit
MsgBox "Process Ok"

End Sub```


在循环中,测试是否找到选项卡,即如果没有选项卡,则执行操作。。。。。否则移动到下一个id。将SET行包裹在On Error Resume next On Error GoTo 0对中,以抑制潜在的元素未找到异常。

我认为linha应该是行和数字用来设置下一个id。

请注意,使用不合格的范围(例如Range)而不指定工作表,即隐含地引用当前ActiveSheet,这很容易出错。

Do Until Cells(Line, 1) = vbNullString
For Line = 4 To tell

Number = Range("A" & Line).Value
Dim targetTab As webElement

On Error Resume Next 'suppress potential exception
Set targetTab = drive.FindElementById("WIDGET_ID_4")     
On Error GoTo 0 'switch exception raising back on
If Not targetTab Is Nothing Then 'test if reference obtained for target element

drive.SwitchToFrame targetTab
Set telephone = drive.FindElementByXPath("/html/body/.........")
Range("B" & Line).Value = telephone.Attribute("outerText")

drive.SwitchToDefaultContent
Set targetTab = Nothing
End If
Next Line
Loop

最新更新