VBA代码在调试模式下工作,但在运行模式下失败

时间:2018-02-12 09:21:47

标签: vba excel-vba ie-automation excel

我正在尝试填充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

2 个答案:

答案 0 :(得分:0)

DoEvents放在现在延误的位置。这可能会从调试中重新创建这种情况。

答案 1 :(得分:0)

所以最后在尝试各种解决方案后,我发现代码运行得比它应该的更快。我设法通过添加一些额外的等待句来解决问题。另外我使用单独的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。