VBA代码在调试模式下工作,但在运行模式下失败 [英] VBA code works in debug mode, but fails in run mode

查看:81
本文介绍了VBA代码在调试模式下工作,但在运行模式下失败的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am trying to fill one input field in Internet explorer, but the code is only working in debug mode, while I am pressing F8 on each of the actions. When I am running the code automatically, the value fill is not working, the input field remains empty.

可能是什么问题?

这是代码的HTML部分:

Here is the HTML part of the code:

<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填充该字段

I am trying to fill the field with ID input-amount_get

下面是我尝试过的选项,我注释掉了其他选项,因为它们没有帮助.就像我说的那样,当我使用F8浏览代码的这一部分时,它就可以正常工作.

Below are the options that i have tried, I commented out the other options, as they did not help. As i said, when I am going through this part of the code with F8, it works just fine.

 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"))

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

Here is the full code that I am using:

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

推荐答案

因此,在尝试了各种解决方案之后,我终于发现,代码以某种方式运行得比预期的快.我设法通过添加一些其他的等待语句来解决该问题.另外,我使用单独的Private Sub来设置延迟:

So finally after experimenting with various solutions I found out that somehow the code was running faster than it should. I managed to solve the issue by adding some additional wait sentences. Also I am using separate Private Sub for setting the delay:

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

现在输入文本的代码行如下所示:

And the code lines for entering the text now look like:

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

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

When these lines are reached, the code waits for 4 additional seconds and then sets the value to 500.

这篇关于VBA代码在调试模式下工作,但在运行模式下失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆