【问题标题】:IE automation through VBA - wait for element to disappear通过 VBA 实现 IE 自动化 - 等待元素消失
【发布时间】:2018-02-13 16:01:30
【问题描述】:

我正在填写一个网络表单,其中有一个内置的预加载器动画(不会影响浏览器状态),我试图让我的脚本等待它消失,然后才继续填写字段。

目前我正在使用此代码:

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

    objIE.document.getElementsByName("button_submit")(0).Click

不知道为什么,但有时此代码无法正常工作,并且未按下提交按钮 (button_submit)。

我想尽可能避免等待语句,因为在页面加载缓慢时它们是不可靠的。

有没有更好的方法让脚本等待特定元素(在本例中为“preloader__loader”)消失?

提前谢谢你。

【问题讨论】:

  • 包含网络表单的链接可能会有所帮助。
  • 很遗憾,这是不可能的,网站只能通过内网访问
  • 首先删除On Error Resume Next,因为您无法控制如果发生错误会发生什么。创建能够检查所有必要状态以避免错误的代码。例如。检查.getElementsByClassName("preloader__loader").length。

标签: vba excel ie-automation


【解决方案1】:

所以,这就是我所做的,代码的第一部分正在等待 Preloader 出现,当它出现时,第二部分正在等待 Preloader 消失。

这个解决方案现在似乎工作得很好,如果页面加载速度比预期的慢,我希望不会有任何其他问题:)

      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 el Is Nothing

      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)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-08-20
    • 1970-01-01
    • 2018-07-31
    • 2020-01-16
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多