web-dev-qa-db-ja.com

Failproof Wait for IE to load to

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

他に何か提案はありますか?

7
user3408723

数年後、それも私を襲った。私は提案されたソリューションを見て、たくさんテストしました。次のコマンドの組み合わせが開発されました。これをアプリケーションで使用します。

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が最初のループの後であることが判明した後、私がインストールした2番目の同一のループは、.

よろしく、ScriptMan

2
ScriptMan

私は非常に長い間うまく使用してきました:

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

PCを変更してWindows 10とOffice 16に切り替えたときまで、完全に機能していました。その後、一部のケースで機能し始めましたが、ループが完了しない場合がありました。ループのどちらの条件にも到達しなかったため、ループはエンドレスでした。

多くのグーグル操作の後、この投稿で解決策が見つかるまで、私は多くの提案を試みました: Excel VBA Controlling IE local intranet

解決策は、URLをInternet Explorerの[セキュリティ]タブの信頼済みサイトリストに追加することです。最後に!

2
user3254924

これを試してください、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

PD:ドリルダウンIEイベントが見つかりましたIE_DocumentCompleteは、ページが実際に準備される前の最後のイベントです。したがって、Webページが読み込まれたときに検出する方法がもう1つあります(リダイレクトの場合など、ターゲット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
omegastripes

このスクリプトを上に配置してください。これで問題が解決する可能性があります。

{ 
    $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が読み込まれていないため、スクリプトはパラメーターを検出しません。

1

したがって、この回答を調べても、ページが完全に読み込まれるのを待つのに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ミリ秒間隔で待機することです。もちろん、これは必要に応じて調整可能ですが、私のアプリケーションではそれでうまくいきました。

0
Poozy101

フォームの送信時に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

違いは、あなたの参照はIEオブジェクトの後にwscript.sleep。オブジェクトがロードされる前に、何よりもまずそれらをチェックする場合。スクリプトが失敗する可能性があります。

0
Rich