Detect when a web page is loaded without using sleep
The code below, which came from code in the "UPD2:" section of the above link, does not work. The objIE.getProperty is returning nothing, so it never matches the first tokenized instance strSignature. I was able to isolate that the For Each statement is not locating the open browser instance. Can you please help me fix the code?
I'm on Windows 7 with IE9. My company will soon be upgrading to IE11. I don't think that makes any difference, as I was able to validate that the initial tokenized strSignature is working and being retained. It's just not locating the open browser window and therefore unable to grab the second token for comparison.
For Each objIE In CreateObject("Shell.Application").Windows
' loop through all explorer windows to find tokenized instance
If objIE.GetProperty("marker") = strSignature
As requested here is the full code/paste from the reference link above to make the question more self-contained.
'UPD2: You wrote that IE gets disconnected as the login pop-up comes in, hypothetically there is a way to catch disconnection, and then get IE instance again. Note this is "abnormal programming" :) I hope this helps:
Option Explicit
Dim objIE, strSignature, strInitType
Set objIE = CreateObject("InternetExplorer.Application") ' create IE instance
objIE.Visible = True
strSignature = Left(CreateObject("Scriptlet.TypeLib").GUID, 38) ' generate uid
objIE.putproperty "marker", strSignature ' tokenize the instance
strInitType = TypeName(objIE) ' get typename
objIE.Navigate "https://www.yahoo.com/"
MsgBox "Initial type = " & TypeName(objIE) ' for visualisation
On Error Resume Next
Do While TypeName(objIE) = strInitType ' wait until typename changes (ActveX disconnection), may cause error 800A000E if not within OERN
WScript.Sleep 10
Loop
MsgBox "Changed type = " & TypeName(objIE) ' for visualisation
Set objIE = Nothing ' excessive statement, just for clearance
Do
For Each objIE In CreateObject("Shell.Application").Windows ' loop through all explorer windows to find tokenized instance
If objIE.getproperty("marker") = strSignature Then ' our instance found
If TypeName(objIE) = strInitType Then Exit Do ' may be excessive type check
End If
Next
WScript.Sleep 10
Loop
MsgBox "Found type = " & TypeName(objIE) ' for visualisation
On Error GoTo 0
Do While objIE.ReadyState <> 4 ' conventional wait if instance not ready
WScript.Sleep 10
Loop
MsgBox "Title = " & objIE.Document.Title ' for visualisation