Detect when a web page is loaded without using sleep
Asked Answered
V

2

5

I am creating a VB script on windows which opens a site in IE. What I want: Detect when the web page is loaded and display a message. I achieved this by using sleep (WScript.Sleep) for approx. seconds when the site gets loaded. However, the site pops up user name, password in the midway. Only when the user enter credentials, it finishes loading the page. So I don't want to use "sleep" for approx seconds, instead an exact function or a way to detect that the page got loaded. I checked on line and tried using Do While loop, onload, onclick functions, but nothing works. To simplify, even if I write a script to open a site like yahoo and detect, display a message "Hi" when the page is loaded: It doesn't work without using sleep (WScript.Sleep).

Vladikavkaz answered 23/4, 2014 at 0:5 Comment(0)
B
6

Try conventional method:

Set objIE = CreateObject("InternetExplorer.Application")
objIE.Visible = True
objIE.Navigate "https://www.yahoo.com/"
Do While objIE.ReadyState <> 4
    WScript.Sleep 10
Loop
' your code here
' ...

UPD: this one should check for errors:

Set objIE = CreateObject("InternetExplorer.Application")
objIE.Visible = True
objIE.Navigate "https://www.yahoo.com/"
On Error Resume Next
Do 
    If objIE.ReadyState = 4 Then
        If Err = 0 Then
            Exit Do
        Else
            Err.Clear
        End If
    End If
    WScript.Sleep 10
Loop
On Error Goto 0
' your code here
' ...

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

You can get all text nodes, links etc. from DOM, as follows:

Option Explicit
Dim objIE, colTags, strResult, objTag, objChild, arrResult

Set objIE = CreateObject("InternetExplorer.Application")
objIE.Visible = True
objIE.Navigate "https://www.yahoo.com/"

Do While objIE.ReadyState <> 4
    WScript.Sleep 10
Loop

Set colTags = objIE.Document.GetElementsByTagName("a")
strResult = "Total " & colTags.Length & " DOM Anchor Nodes:" & vbCrLf
For Each objTag In colTags
    strResult = strResult & objTag.GetAttribute("href") & vbCrLf
Next
ShowInNotepad strResult

Set colTags = objIE.Document.GetElementsByTagName("*")
arrResult = Array()
For Each objTag In colTags
    For Each objChild In objTag.ChildNodes
        If objChild.NodeType = 3 Then
            ReDim Preserve arrResult(UBound(arrResult) + 1)
            arrResult(UBound(arrResult)) = objChild.NodeValue
        End If
    Next
Next
strResult = "Total " & colTags.Length & " DOM object nodes + total " & UBound(arrResult) + 1 & " #text nodes:" & vbCrLf
strResult = strResult & Join(arrResult, vbCrLf)
ShowInNotepad strResult

objIE.Quit

Sub ShowInNotepad(strToFile)
    Dim strTempPath
    With CreateObject("Scripting.FileSystemObject")
        strTempPath = CreateObject("WScript.Shell").ExpandEnvironmentStrings("%TEMP%") & "\" & .gettempname
        With .CreateTextFile(strTempPath, True, True)
            .WriteLine (strToFile)
            .Close
        End With
        CreateObject("WScript.Shell").Run "notepad.exe " & strTempPath, 1, True
        .DeleteFile (strTempPath)
    End With
End Sub

Also look get text data

UPD3: I want to place here additional check if webpage loading and initialization are completed:

' ...
' Navigating to some url
objIE.Navigate strUrl
' Wait for IE ready
Do While objIE.ReadyState <> 4 Or objIE.Busy
    WScript.Sleep 10
Loop
' Wait for document complete
Do While objIE.Document.ReadyState <> "complete"
    WScript.Sleep 10
Loop
' Processing loaded webpage code
' ...

UPD4: There are some cases when you need to track if a target node have been created in the document (usually it's necessary if you get Object required error while attempting to access the node by .getElementById, etc.):

If the page uses AJAX (loaded page source HTML doesn't contain target node, active content like JavaScript creates it dynamically), there is the example in the below snippet of a page, showing how that could look like. The text node 5.99 might be created after the page was completely loaded, and some other requests to a server for extra data to be displayed have taken a place:

...
<td class="price-label">
    <span id="priceblock" class="price-big color">
        5.99
    </span>
</td>
...

Or if you are loading e. g. Google search result page and waiting for Next button is appeared (especially, if you invoked .click method on the previous page), or loading some page with login web form and waiting for username input field like <input name="userID" id="userID" type="text" maxlength="24" required="" placeholder="Username" autofocus="">.

The below code allows to make an additional check if the target node is accessible:

With objIE
    ' Navigating to some url
    .Navigate strUrl
    ' Wait for IE ready
    Do While .ReadyState <> 4 Or .Busy
        WScript.Sleep 10
    Loop
    ' Wait for document complete
    Do While .Document.ReadyState <> "complete"
        WScript.Sleep 10
    Loop
    ' Wait for target node created
    Do While TypeName(.Document.getElementById("userID")) = "Null"
        WScript.Sleep 10
    Loop
    ' Processing target node
    .Document.getElementById("userID").Value = "myusername"
    ' ...
    '
End With
Beautician answered 23/4, 2014 at 0:15 Comment(14)
Thanks, it worked fine with the yahoo site as required. However, I am using a site which pops up credential box in the mid way of loading the web page. When the credentials are entered, it finishes loading the page. When I changed to that site in this code with yahoo.com, it gave me the error on Do While statement: Line 4, char 1 "The object invoked has disconnected from its clients". Code: 80010108. If I continue to login to load that web page, it doesn't detect that web page got loaded.Vladikavkaz
Please try my updated code, if your page seems to be completely loaded but no reaction from script - check if wscript.exe is running in processes, if yes - probably it may get a permanent error that causes infinite loop.Beautician
Thanks once again, this loads the web page but doesn't display the message after detecting that webpage loaded. So, based on the site and requirement I have, let me modify my query: Script opens "login.yahoo.com/config/login_verify2?" in IE. When the credentials are entered and the page is further loaded showing emails, it should detect and display a message, say "Here are your emails". This is what I am trying to achieve.Vladikavkaz
Just to add here, I tested it with login.yahoo.com/config/login_verify2?%22 as well, it works fine. But the web page I am using, is a .asp page. Does this have something to do with the code not working for that page?Vladikavkaz
It's no matter if the page is .asp or another one, 'cause your browser works with pages after parsing html received via http requests. Please share the web address example with which that code doesn't work properly.Beautician
I am exactly looking for this (To share a site as an example), will do as I find it. However, let me share the behavior of the site: The login window asking for user name, password comes as a popup while the URL is loading. Means site loading is paused as that popup appears and when credentials are entered, then the site loads (Normally sites ask for login after they've loaded and there the code you shared works fine). In this case, since the site loading is paused, the code gets terminated, giving the message: "Object initiated got disconnected from it's clients".Vladikavkaz
I tested it further and found that the code assumes IE to be ready when the login popup appears. Hence, would it be possible to get the code detect popup window instead? Again: I will share such site example, as I find any at my end.Vladikavkaz
There is the similar question, may be this helps.Beautician
Thanks. I checked that, as I mentioned the object IE gets disconnected as the login pop-up comes in. Could you please provide a way to detect/ read text or URL content in an already loaded web page...Vladikavkaz
I see something of thos sort here (community.spiceworks.com/topic/…), but I need to get it working for asp web page.Vladikavkaz
I've added suggestions to my answer how to get disconnected IE and links | text nodes from DOM.Beautician
@Sriram Take a look at my UPD4, it may be useful.Beautician
@Beautician Thanks. I did updated in a similar way. Document.ReadyState = "complete" worked like a charm. Wrote a function and returned when object found. 100% working. Thanks again.Unceremonious
BTW, there are similar solutions for VBA: 1, 2, 3.Beautician
P
0

The Following Check by Element Solved for me :

Function waitLoadByElement(p_ElementName)

Do While IE.ReadyState <> 4 Or IE.Busy
        WScript.Sleep 1000
    Loop

    Do While IE.Document.ReadyState <> "complete"
        WScript.Sleep 1000
    Loop

       ' This is the interesting part

    Do While (instr(IE.document.getElementsByTagName("body")(0).InnerHTML,p_ElementName) < 1 )
    v_counter = v_counter + 1

        WScript.Sleep 1000
    Loop
    On Error GoTo 0

    if v_counter > 0 then
        MyEcho "[ Waited Object to Load ] : " & v_counter & " - Seconds"
    end if

End Function
Popularly answered 1/4, 2019 at 14:17 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.