Selenium Webdriver VBA-新循环错误

发布于 2025-02-09 15:41:13 字数 773 浏览 1 评论 0原文

晚安,

如果可能的话,我希望一些帮助。我将此代码放在一起,但是如果找不到页面项目,我将无法创建输出。我希望,如果他没有找到Intem,他会去下一行,因为我有很多数据要捕获,因此不会显示错误消息。


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```




Goodnight

I would like some help if possible. I'm putting this code together, but I'm not able to create an output if it doesn't find the page item. I would like if he doesn't find the intem he would go to the next line, not displaying an error message as I have a lot of data to capture.


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```




如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

入怼 2025-02-16 15:41:13

在循环中,测试是否找到“选项卡”,即如果不是 tab 是什么都不做的。将设定线包装在一个错误简历中,下一个错误goto 0对,以抑制未发现异常的潜在元素。

我认为Linha应该是行,并使用数字来设置下一个ID。

请注意,使用不合格的范围,例如,没有指定工作表,您隐含地引用了当前的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

Within the loop, test if the tab is found i.e. if not tab is nothing then do stuff.....else move to next id. Wrap the SET line in an On Error Resume Next On Error GoTo 0 pair to suppress potential element not found exception.

I think linha should be line and Number be used to set the next id.

Please do note that using unqualified ranges e.g. Range without specifying the worksheet, you are implicitly referencing the current ActiveSheet and this is bug-prone.

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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文