在Excel-VBA中实现等待元素可用

时间:2016-08-17 19:37:12

标签: excel-vba web-scraping vba excel

我尝试实现等待元素可用(在Stackoverflow本身中找到)但收到错误:

  

运行时错误'-2147467259(80004005)':
自动化错误
未指定错误

以下是代码段:

Do
    Set testObject = Nothing
    On Error Resume Next
    Set testObject = html.getElementById("j_username")
    On Error GoTo 0
    DoEvents
Loop While testObject Is Nothing

1 个答案:

答案 0 :(得分:0)

您收到错误,因为HTML文档尚未完全加载。在你的do循环中添加:

Do Until IE.ReadyState = READYSTATE_COMPLETE
    DoEvents
Loop
Set html = IE.Document

只有在正确引用加载的对象后才有意义:

Set testObject = html.getElementById("j_username")