-->

Failproof等待IE加载Failproof等待IE加载(Failproof Wait for

2019-05-12 03:09发布

是否有脚本等到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

任何其他的建议?

Answer 1:

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


Answer 2:

我有过成功使用了很长时间:

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

它已经完美的工作,直到今天,当我改变了我的电脑,并切换到Windows 10和Office 16,然后,它开始在某些情况下工作,但在未完成的循环有过那么几次。 在循环的条件一个都没有达到,因此该循环是无止境的。

很多谷歌搜索后,我已经尝试了许多建议,直到我发现这个职位的解决方案: Excel的VBA控制IE本地Intranet

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



Answer 3:

尽量把这个脚本的顶部,这可能会解决你的问题。

{ 
    $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;
    }
}

说明:

当你从正常模式下运行脚本,所以它不读书IE状态正确,这就是为什么DOM没有被加载,从而PowerShell是不是有一些权利,脚本没有找到任何参数



Answer 4:

几年后,还打我。 我看了看提出的解决方案,并测试了很多。 下面的命令的组合已经研制成功,这是我现在在我的应用程序中使用。

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 =真是第一循环之后有时。

问候,ScriptMan



Answer 5:

因此,通过查找这个答案,我仍然有与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的间隔加载停止增加标记名数。 当然,这是可调的,取决于你想要什么,但工作了我的申请。



Answer 6:

如果你用IE浏览器上的表单的提交工作,最好把它放在一个子,所以你可以多次引用同一个小组。

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

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



文章来源: Failproof Wait for IE to load