VBA Internet Explorer 宏 - 代码 运行 很好地调试了 1 by 1 命令,但如果我 运行 整个程序则不起作用

VBA Macro for Internet Explorer - code run well on debugging 1 by 1 command but didn't work if i run the whole program

我刚开始使用 VBA 用于自动化 Internet Explorer 的宏。

我用它来创建机器人来关注其他在线购物追随者(电子商务)。 实际上,如果我使用 debug 1 by 1 命令,代码 运行 很好,并且它有效(可以执行以下操作)。 但是如果我 运行 整个程序,代码可能只运行几个命令并停止(但没有错误)。

我将其用于电子商务 (sho.ee)。 步骤是:

如果我 运行 一步一步就成功了。 希望大家帮我看看代码哪里错了

这是代码=>我给xxx改了店名

Sub Followers()

    Dim objIE As InternetExplorer
    Dim ieAnchors As Object
    Dim Anchor As Object


    Set objIE = New InternetExplorer

    objIE.Visible = True
    objIE.Navigate "https://shopee.co.id/shop/xxx/followers/"

    Do While objIE.Busy = True Or objIE.ReadyState <> 4: DoEvents: Loop
    Set ieAnchors = objIE.Document.getElementsByClassName("shop-href")

   For Each Anchor In ieAnchors

        result = Anchor

        If result <> result_prev Then

           Dim objIE_1 As InternetExplorer
           Dim ieBtn As Object
           Dim Btn As Object

           Set objIE_1 = New InternetExplorer
           objIE_1.Visible = True
           objIE_1.Navigate Anchor

           Do While objIE_1.Busy = True Or objIE_1.ReadyState <> 4: DoEvents: Loop

           Set ieBtn = objIE_1.Document.getElementsByClassName("shopee-button-outline shopee-button-outline--complement shopee-button-outline--fill ")

                      For Each Btn In ieBtn
                      Btn.Click
                      Next Btn

           objIE_1.Quit

        result_prev = Anchor

        End If

   Next Anchor

End Sub

有时页面会分部分加载,在加载所有元素之前注册为完整。在这些情况下,我使用 application.Wait 强制程序等待一段时间以加载页面:

'wait five seconds
Application.Wait (Now + TimeValue("0:00:05"))

如果它是一个简单的页面,我将使用 Do 循环检查 ReadyState 以避免不必要地减慢程序速度:

'wait for page to load
Do Until ie.ReadyState = READYSTATE_COMPLETE or ie.ReadyState = 4
    DoEvents
Loop

是的,它起作用了。通过添加 Application.Wait (Now + TimeValue("0:00:05"))

子关注者()

Dim objIE As InternetExplorer
Dim ieAnchors As Object
Dim Anchor As Object


Set objIE = New InternetExplorer

objIE.Visible = True
objIE.Navigate "https://shopee.co.id/shop/xxx/followers/"

Do While objIE.Busy = True Or objIE.ReadyState <> 4: DoEvents: Loop

Application.Wait(现在+时间值(“0:00:02”)) 设置 ieAnchors = objIE.Document.getElementsByClassName("shop-href")

对于ieAnchors中的每个锚点

    result = Anchor

    If result <> result_prev Then

       Dim objIE_1 As InternetExplorer
       Dim ieBtn As Object
       Dim Btn As Object

       Application.Wait (Now + TimeValue("0:00:02")) 
       Set objIE_1 = New InternetExplorer
       objIE_1.Visible = True
       objIE_1.Navigate Anchor

       Do While objIE_1.Busy = True Or objIE_1.ReadyState <> 4: DoEvents: Loop

       Set ieBtn = objIE_1.Document.getElementsByClassName("shopee-button-outline shopee-button-outline--complement shopee-button-outline--fill ")

                  For Each Btn In ieBtn
                  Btn.Click
                  Next Btn

       objIE_1.Quit

    result_prev = Anchor

    End If

下一个主播

结束子