【问题标题】:VBA code works in debug mode, but fails in run modeVBA 代码在调试模式下工作,但在运行模式下失败
【发布时间】:2018-02-12 09:21:46
【问题描述】:

我正在尝试在 Internet Explorer 中填写一个输入字段,但代码仅在调试模式下工作,而我在每个操作上都按 F8。当我自动运行代码时,值填充不起作用,输入字段保持为空。

可能是什么问题?

这是代码的 HTML 部分:

<input tabindex="0" class="select__input" type="text">
</div>
      <div class="sm__amount" name="payment">
          <div class="sm__send">
              <div class="amount selected" id="amount_get">
                   <div class="amount__field">
                      <div class="amount__label selected">Send amount</div>
      <input id="input-amount_get" type="text" value="" autoComplete="off">
                      </div>
                      <div class="amount__select" id="amount-select">
                          <div class="amount__select__value">
                             XXX
                              <div class="amount__select__trl"></div>
                           </div>
                           <div class="amount__select__box">
                               <div id="currency-XXX" data-v="XXX">XXX</div>
                               <div id="currency-XXX" data-v="XXX"XXX</div>
                     </div>
               </div>
         </div>
  </div>

我正在尝试使用 ID input-amount_get 填写该字段

以下是我尝试过的选项,我注释掉了其他选项,因为它们没有帮助。正如我所说,当我使用 F8 浏览这部分代码时,它工作得很好。

 Application.Wait (Now + TimeValue("0:00:03"))
    'objIE.document.getElementById("input-amount_get").Focus
    'objIE.document.getElementsByTagName("input")(1).innerText = "500"
    objIE.document.getElementById("input-amount_get").innerText = "500.00"
    'objIE.document.getElementById("input-amount_get").Click
    'objIE.document.getElementById("input-amount_get").FireEvent ("onchange")
    Application.Wait (Now + TimeValue("0:00:05"))

这是我正在使用的完整代码:

Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)

Sub TestBot()

    Dim objIE As InternetExplorer
    Dim doc As HTMLDocument
    Dim el As Object


    Set objIE = New InternetExplorer

    objIE.Visible = True

    objIE.navigate "Website"

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

    Do
        Set el = Nothing
        On Error Resume Next
        Set el = objIE.document.getElementById("login")
        On Error GoTo 0
        DoEvents
        Sleep 500
    Loop While el Is Nothing


    objIE.document.getElementById("login").Click

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

 Do
        Set el = Nothing
        On Error Resume Next
        Set el = objIE.document.getElementsByClassName("button submit")(0)
        On Error GoTo 0
        DoEvents
        Sleep 500
    Loop While el Is Nothing

     Set doc = objIE.document


    objIE.document.getElementsByTagName("input")(0).innerText = "email@email.com"

    objIE.document.getElementsByTagName("input")(3).innerText = "Password"

    objIE.document.getElementsByClassName("button submit")(0).Click


    Do
        Set el = Nothing
        On Error Resume Next
        Set el = objIE.document.getElementById("account_menu")
        On Error GoTo 0
        DoEvents
        Sleep 500
    Loop While el Is Nothing



    objIE.document.getElementsByClassName("profile__button")(0).Click

   Do
        Set el = Nothing
        On Error Resume Next
        Set el = objIE.document.getElementById("wrapBox")
        On Error GoTo 0
        DoEvents
        Sleep 500
    Loop While el Is Nothing

    Application.Wait (Now + TimeValue("0:00:01"))
    objIE.document.getElementsByClassName("select__trl")(0).Click
    Application.Wait (Now + TimeValue("0:00:01"))
    objIE.document.getElementsByName("US")(0).Click

      Do
        Set el = Nothing
        On Error Resume Next
        Set el = objIE.document.getElementsByClassName("preloader__loader")(0)
        On Error GoTo 0
        DoEvents
        Sleep 1000
    Loop While Not (el Is Nothing)

    Do Until objIE.readyState <> 4: DoEvents: Loop

    Application.Wait (Now + TimeValue("0:00:03"))
    DoEvents
    objIE.document.getElementById("input-amount_get").innerText = "500.00"
    Application.Wait (Now + TimeValue("0:00:05"))

    Do
        Set el = Nothing
        On Error Resume Next
        Set el = objIE.document.getElementsByClassName("preloader__loader")(0)
        On Error GoTo 0
        DoEvents
        Sleep 500
    Loop While Not (el Is Nothing)

    Application.Wait (Now + TimeValue("0:00:03"))
    objIE.document.getElementsByName("button_submit")(0).Click


    objIE.Quit

End Sub

【问题讨论】:

  • 感谢您的提示,但使用代码 Do Until objIE.readyState = 4: wscript.Sleep 10: Loop 也没有帮助
  • 请发布完整的程序。
  • 我已经在原帖中添加了。

标签: vba excel ie-automation


【解决方案1】:

DoEvents 放在您现在的延迟位置。这可能会从调试中重现这种情况。

【讨论】:

  • 仍然没有运气:Application.Wait (Now + TimeValue("0:00:03")) DoEvents objIE.document.getElementById("input-amount_get").innerText = "500.00" Application.Wait (Now + TimeValue("0:00:05"))
【解决方案2】:

所以最后在尝试了各种解决方案后,我发现不知何故代码运行得比它应该的要快。我设法通过添加一些额外的等待语句来解决这个问题。我也使用单独的 Private Sub 来设置延迟:

Private Sub delay(seconds As Long)
    Dim endTime As Date
    endTime = DateAdd("s", seconds, Now())
    Do While Now() < endTime
        DoEvents
    Loop
End Sub

输入文本的代码行现在看起来像:

DoEvents 'not necessary, but I left it just in case
delay 4
objIE.document.getElementById("input-amount_get").innerText = "500"

当到达这些行时,代码会再等待 4 秒,然后将值设置为 500。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-03-01
    • 1970-01-01
    • 1970-01-01
    • 2021-06-10
    • 2017-07-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多