Failproof等待IE加载

问题描述 投票:7回答:6

是否有一种万无一失的方式让脚本等到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

还有其他建议吗?

internet-explorer web-scraping vbscript wsh readystate
6个回答
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

UPD:向下钻取IE事件我发现IE_DocumentComplete是页面实际准备好之前的最后一个事件。因此,还有一种方法可以检测何时加载网页(请注意,您必须指定可能与目标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

2
投票

我已经很长时间成功使用了:

While IE.readyState <> 4 Or IE.Busy: DoEvents: Wend

它一直工作到今天,当我改变我的PC并切换到Windows 10和Office 16.然后它开始处理一些情况,但有时循环没有完成。循环中的任何一个条件都没有到达,因此循环是ENDLESS。

经过大量的谷歌搜索,我已经尝试了很多建议,直到我在这篇文章中找到解决方案:Excel VBA Controlling IE local intranet

解决方案是将URL添加到Internet Explorer安全性选项卡中的受信任站点列表中。最后!


2
投票

几年后,它也打击了我。我查看了提出的解决方案并进行了大量测试。已经开发了以下命令组合,我现在将在我的应用程序中使用它们。

Set oIE = CreateObject("InternetExplorer.application")

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

wscript.sleep 100
Do While oIE.Busy or oIE.ReadyState <> 4: WScript.Sleep 100: Loop  

wscript.sleep 100
Do While oIE.Busy or oIE.ReadyState <> 4: WScript.Sleep 100: Loop  

msgbox oIE.ReadyState & " / " & oIE.Busy , vbInformation +  vbMsgBoxForeground , "Information"

oIE.Quit

set oIE = Nothing

我发现第二个完全相同的循环是在第一个循环之后有时候oIE.Busy = True。

此致,ScriptMan


1
投票

尝试将此脚本放在顶部,这可能会解决您的问题。

{ 
    $myWindowsID = [System.Security.Principal.WindowsIdentity]::GetCurrent();
    $myWindowsPrincipal = New-Object System.Security.Principal.WindowsPrincipal($myWindowsID);
    $adminRole = [System.Security.Principal.WindowsBuiltInRole]::Administrator;

    if ($myWindowsPrincipal.IsInRole($adminRole)) {
        $Host.UI.RawUI.WindowTitle = $myInvocation.MyCommand.Definition + "(Elevated)";
        Clear-Host;
    }
    else {
        $newProcess = New-Object System.Diagnostics.ProcessStartInfo "PowerShell";
        $newProcess.Arguments = "& '" + $script:MyInvocation.MyCommand.Path + "'"
        $newProcess.Verb = "runas";
        [System.Diagnostics.Process]::Start($newProcess);
        Exit;
    }
}

说明:

当您从正常模式运行脚本时,Powershell没有一些权限,因此它没有正确读取IE状态,这就是为什么没有加载DOM的原因,脚本没有找到任何参数


0
投票

所以通过查找这个答案,我仍然遇到IE等待页面完全加载的麻烦。希望这个解决方案能够帮助他人,这就是我所做的:

Dim i As Integer
Dim j As Integer
Dim tagnames As Integer

While ie.Busy
    DoEvents
Wend
While ie.ReadyState <> 4
    DoEvents
Wend

i = 0
j = 0
While (i <> 5)
    i = 0
    tagnames = ie.document.getelementsbytagname("*").Length
    For j = 1 To 5
        Sleep (50)
        If tagnames = ie.document.getelementsbytagname("*").Length Then
            b = b + 1
        End If
    Next j
Wend

基本上,它的作用是等待5个50ms的间隔,以便加载的标记名数量停止增加。当然,这可以根据你的需要进行调整,但这对我的应用程序有效。


0
投票

如果您在表单提交时使用IE,最好将其放在Sub中,以便您可以重复引用相同的Sub。

Dim IE
Set IE = WScript.CreateObject("InternetExplorer.Application")
    IE.Visible = True
    IE.Navigate "http://www.google.com"
    Wait IE, 500

Sub Wait(IE, SleepInterval)
    Do
        WScript.Sleep SleepInterval
    Loop While IE.ReadyState < 4 Or IE.Busy
End Sub

不同的是,您在wscript.sleep之后引用IE对象。如果在加载对象之前首先检查它们。它可能导致脚本失败。

© www.soinside.com 2019 - 2024. All rights reserved.