Failproof等待IE加载

use*_*723 7 vbscript internet-explorer wsh readystate web-scraping

是否有一种万无一失的方式让脚本等到Internet Explorer完全加载?

两者oIE.Busy和/或oIE.ReadyState都没有按照他们应该的方式工作:

Set oIE = CreateObject("InternetExplorer.application")

    oIE.Visible = True
    oIE.navigate ("http://technopedia.com")

    Do While oIE.Busy Or oIE.ReadyState <> 4: WScript.Sleep 100: Loop  

    ' <<<<< OR >>>>>>

    Do While oIE.ReadyState <> 4: WScript.Sleep 100: Loop
Run Code Online (Sandbox Code Playgroud)

还有其他建议吗?

ome*_*pes 2

试试这个,它帮助我解决了一次 IE 的类似问题:

Set oIE = CreateObject("InternetExplorer.application")
oIE.Visible = True
oIE.navigate ("http://technopedia.com")
Do While oIE.ReadyState = 4: WScript.Sleep 100: Loop
Do While oIE.ReadyState <> 4: WScript.Sleep 100: Loop
' example ref to DOM
MsgBox oIE.Document.GetElementsByTagName("div").Length
Run Code Online (Sandbox Code Playgroud)

UPD:深入研究 IE 事件,我发现这IE_DocumentComplete是页面实际准备好之前的最后一个事件。因此,还有另一种方法来检测网页何时加载(请注意,您必须指定确切的目标 URL,该 URL 可能与目标 URL 不同,例如在重定向的情况下):

option explicit
dim ie, targurl, desturl, completed

set ie = wscript.createobject("internetexplorer.application", "ie_")
ie.visible = true

targurl = "http://technopedia.com/"
desturl = "http://technopedia.com/"

' targurl = "http://tumblr.com/"
' desturl = "https://www.tumblr.com/" ' redirection if you are not login
' desturl = "https://www.tumblr.com/dashboard" ' redirection if you are login

completed = false
ie.navigate targurl
do until completed
    wscript.sleep 100
loop
' your code here
msgbox ie.document.getelementsbytagname("*").length
ie.quit

sub ie_documentcomplete(byval pdisp, byval url)
    if url = desturl then completed = true
end sub
Run Code Online (Sandbox Code Playgroud)