internet explorer-防故障等待IE加载



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

还有其他建议吗?

试试这个,它帮助我解决了一次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

很长一段时间以来,我一直在成功地使用:

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

它一直运行得很好,直到今天,我换了电脑,换成了Windows 10和Office 16。然后它开始处理某些情况,但有时循环还没有完成。循环中的任何一个条件都没有达到,因此循环是无止境的。

经过大量的谷歌搜索,我尝试了许多建议,直到我在这篇文章中找到了解决方案:Excel VBA控制IE本地intranet

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

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

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

我安装的第二个相同的循环是在第一个循环之后。

谨致问候,ScriptMan

试着把这个脚本放在上面,这可能会解决你的问题。

{ 
    $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没有被加载因此,脚本没有找到任何参数

所以通过查找这个答案,我在等待页面完全加载时仍然遇到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的间隔,等待加载的标记名数量停止增加。当然,这是可以根据您的需要进行调整的,但这对我的应用程序有效。

如果您在表单提交中使用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对象。如果在加载对象之前首先检查它们。这可能会导致脚本失败。

最新更新