Failproof Wait for IE to load
Asked Answered
L

6

10

Is there a foolproof way for the script to wait till the Internet explorer is completely loaded?

Both oIE.Busy and / or oIE.ReadyState are not working the way they should:

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

Any other suggestions?

Lynsey answered 25/4, 2014 at 17:6 Comment(7)
Describe please what's working wrong with conventional Do ... Loop that you've shown?Believe
Looks like it doesn't wait enough for the page to load before carrying on with other actions thus giving "Element required ERROR". When I increased the wait time explicitly, it worked fine.Lynsey
I found the similar behavior once, the solution was just adding the additional loop before that, like Do While oIE.ReadyState = 4: WScript.Sleep 100: Loop. So first step is to wait until the "navigaiting begins", and second - until it to be completed.Believe
I'm not sure what you're suggesting.. Can you elaborate Please?Lynsey
Can you please share the code that gives you "Element required ERROR"? Some DOM nodes may appear later than page was loaded, being dynamically added by scripts (see DHTML). So another way is to perform the preliminary checks before the interaction with some node.Believe
@Lynsey Can't believe I am commenting on something from 2014... But I was having the same issue and omega's answer actually makes sense. The line of code prior to the main loop is in the event that the code hits your loop prior to IE actually processing the request - hence, prior to it becoming Busy. The code is running too fast and hits your loop before IE has the chance to become busy, so therefore it bypasses the loop since it's technically not busy. Very intelligent look into the answer, one that I would have never even considered.Tarsuss
I know this is ageing but I found this via a search and must say I was having the same problems.. I use scriptmans code and it worksCornelius
D
3

I have for a very long time been successfully using:

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

It has been working perfectly until today, when I changed my PC and switched to Windows 10 and Office 16. Then it started working on some cases, but there were times when the loop was not completed. Neither one of the conditions in the loop was reached, so the loop was ENDLESS.

After a lot of Googling, I have tried many suggestions until I found the solution in this post: Excel VBA Controlling IE local intranet

The solution is to add the URL to the trusted sites list in Internet Explorer Security tab. Finally!

Denti answered 14/2, 2016 at 21:51 Comment(0)
B
2

Try this one, it helped me to solve similar problem with IE once:

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: Drilling down IE events I found that IE_DocumentComplete is the last event before the page is actually ready. So there is one more method to detect when a web page is loaded (note that you have to specify the exact destination URL which may differ from the target URL eg in case of redirection):

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
Believe answered 25/4, 2014 at 18:58 Comment(1)
There is the similar question, may be this helps.Believe
P
2

A few years later, it also hit me. I looked at the proposed solutions and tested a lot. The following combination of commands has been developed, which I will now use in my application.

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

The second identical loop I did install after it turned out that oIE.Busy = True was sometimes after the first loop.

Regards, ScriptMan

Preterit answered 2/10, 2017 at 8:40 Comment(0)
F
1

try to put this script on the top, this may solve Your problem.

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

Explanation:

Powershell is not having some rights when you are running script from the normal mode so it is not reading IE status properly and that is why DOM is not being loaded so, script doesn't found any parameter

Fret answered 23/3, 2017 at 4:14 Comment(0)
P
0

If your working with IE on a form submission, it's better to place it in a Sub so you can reference the same Sub repeatedly.

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

The difference being, that your referencing the IE object AFTER the wscript.sleep. If you check them first and foremost before the object is loaded. It could cause script failure.

Parental answered 25/4, 2014 at 19:4 Comment(3)
It should be Loop While IE.ReadyState < 4 Or IE.BusyBelieve
Updated. Thanks omega.Parental
You are missing a closing parenthesis at the end of line 2.Fonseca
G
0

So through looking up this answer, I still had trouble with IE waiting for the page to completely load. In the hopes that this solution will help others, here is what I did:

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

Basically, what this does is wait for 5 50ms intervals for the number of tagnames loaded to stop increasing. Of course, this is adjustable depending on what you want, but that worked for my application.

Geibel answered 16/10, 2015 at 6:12 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.