Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Failproof Wait for IE to load

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?

like image 600
user3408723 Avatar asked Apr 25 '14 17:04

user3408723


2 Answers

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
like image 100
omegastripes Avatar answered Nov 04 '22 11:11

omegastripes


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!

like image 28
user3254924 Avatar answered Nov 04 '22 10:11

user3254924