Detect if a web page has completed loading via IE COM calls with check for ReadyState=4 and Busy=False

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
User avatar
JoeWinograd
Posts: 2198
Joined: 10 Feb 2014, 20:00
Location: U.S. Central Time Zone

Detect if a web page has completed loading via IE COM calls with check for ReadyState=4 and Busy=False

14 Aug 2018, 11:30

I'm using this script to detect if a page has completed loading:

Code: Select all

ReadyState:=0
Busy:=True
ReadyState_Complete:=4
oIE:=ComObjCreate("InternetExplorer.Application")
oIE.Visible:=True
oIE.Navigate("https://www.mlb.com/")
While ((ReadyState<>ReadyState_Complete) or (Busy))
{
  ReadyState:=oIE.ReadyState
  Busy:=oIE.Busy
  Sleep,50
}
MsgBox,4096,Page loaded,ReadyState=%ReadyState%  Busy=%Busy%
oIE.Quit ; comment out if you don't want to close it
ExitApp
While it seems to work on some sites, it clearly is not working at the site in the script above, as the MsgBox appears well before the entire page is loaded. Anyone know how to fix the script above or have a better way to check if a web page has completed loading? Thanks, Joe
User avatar
TheDewd
Posts: 1510
Joined: 19 Dec 2013, 11:16
Location: USA

Re: Detect if a web page has completed loading via IE COM calls with check for ReadyState=4 and Busy=False

14 Aug 2018, 16:06

This works for me:

Code: Select all

IE := ComObjCreate("InternetExplorer.Application")
IE.Visible := 1
IE.Navigate("https://www.mlb.com/")

While (IE.ReadyState <> 4) || (IE.Busy) {
	Sleep, 100
}

MsgBox, 4096, Page Loaded, % "ReadyState=" IE.ReadyState " Busy=" IE.Busy

IE.Quit
ExitApp
User avatar
JoeWinograd
Posts: 2198
Joined: 10 Feb 2014, 20:00
Location: U.S. Central Time Zone

Re: Detect if a web page has completed loading via IE COM calls with check for ReadyState=4 and Busy=False

14 Aug 2018, 17:01

Hi TheDewd,

The only differences between our scripts are:

(1) I assigned the value of ReadyState_Complete (which is 4) to a variable instead of hard-coding it. Doesn't matter.

(2) I use or for logical OR; you use ||. Doesn't matter.

(3) I assigned ReadyState and Busy to variables. Doesn't matter.

(4) I reassign ReadyState and Busy inside the While loop, which I would think is necessary, although I could be wrong about that. Your code doesn't reassign IE.ReadyState or IE.Busy. This may matter.

I ran your code and it has the same problem as mine, i.e., the MsgBox appears before the page is done loading. Here's the result of running your code when the MsgBox appears:
page not loaded.png
page not loaded.png (140.05 KiB) Viewed 2262 times
Here's what it looks like when the page is really done loading:
page loaded.png
page loaded.png (269.55 KiB) Viewed 2262 times
Note the Scores on the left and the graphics at bottom center and bottom right. My code behaves the same as yours. Regards, Joe
User avatar
Xtra
Posts: 2750
Joined: 02 Oct 2015, 12:15

Re: Detect if a web page has completed loading via IE COM calls with check for ReadyState=4 and Busy=False

14 Aug 2018, 17:19

Joe i ran your code 3 times and every time it loaded correctly and then showed message box. (the 3rd time the beer ad wasnt completed but that doesnt matter its coming from another domain)

If you are still having trouble i would check if one of the stat elements exists or has content.
Use a loop with a try block in it and only exit the loop when the conditions are met.

HTH
User avatar
JoeWinograd
Posts: 2198
Joined: 10 Feb 2014, 20:00
Location: U.S. Central Time Zone

Re: Detect if a web page has completed loading via IE COM calls with check for ReadyState=4 and Busy=False

14 Aug 2018, 18:46

Hi Xtra,
i ran your code 3 times
Thanks for testing my code. Your results surprise me, especially since my Internet speed is pretty good...just tested it...257.9 Mbps down, 23.9 up. I can't understand why it loads for you before the MsgBox appears and doesn't for me.
check if one of the stat elements exists or has content
I don't know what you mean by "stat elements"...please explain. Thanks, Joe
User avatar
TheDewd
Posts: 1510
Joined: 19 Dec 2013, 11:16
Location: USA

Re: Detect if a web page has completed loading via IE COM calls with check for ReadyState=4 and Busy=False

14 Aug 2018, 18:58

I think he means "Static". Anyway, the other content may be loading dynamically through JavaScript. So, the page itself might actually be 100% complete with main HTML and scripts, then the scripts begin loading the other content afterwards? Just something to consider.
User avatar
JoeWinograd
Posts: 2198
Joined: 10 Feb 2014, 20:00
Location: U.S. Central Time Zone

Re: Detect if a web page has completed loading via IE COM calls with check for ReadyState=4 and Busy=False

14 Aug 2018, 19:42

TheDewd wrote:the page itself might actually be 100% complete with main HTML and scripts, then the scripts begin loading the other content afterwards
That's an interesting theory! Sounds very plausible to me. Assuming that's what's happening, is there any way to know when the other content (all content!) is done loading? Of course, there are some pages where it's never "done loading"...fresh stuff is always coming in, like ads and new headlines/stories.
User avatar
Xtra
Posts: 2750
Joined: 02 Oct 2015, 12:15

Re: Detect if a web page has completed loading via IE COM calls with check for ReadyState=4 and Busy=False

14 Aug 2018, 19:55

Example to try:

Code: Select all

ReadyState:=0
Busy:=True
ReadyState_Complete:=4
oIE:=ComObjCreate("InternetExplorer.Application")
oIE.Visible:=True
oIE.Navigate("https://www.mlb.com/")

Loop
{
    try
    {
        element := oIE.document.querySelector("#g5-component--mlb-scores--minisb--531203 > a:nth-child(2) > div").innertext
        if (element)
            break
    }
    catch
        Sleep 250
}

;~ While ((ReadyState<>ReadyState_Complete) or (Busy))
;~ {
  ;~ ReadyState:=oIE.ReadyState
  ;~ Busy:=oIE.Busy
  ;~ Sleep,50
;~ }

MsgBox,4096,Page loaded, % element
oIE.Quit ; comment out if you don't want to close it
ExitApp
HTH
User avatar
JoeWinograd
Posts: 2198
Joined: 10 Feb 2014, 20:00
Location: U.S. Central Time Zone

Re: Detect if a web page has completed loading via IE COM calls with check for ReadyState=4 and Busy=False

14 Aug 2018, 23:27

Hi Xtra,
That's a fascinating example...works nicely...but I don't see how it can be extended into a general purpose script that will work at all websites. Good stuff, though. Thanks, Joe

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: Google [Bot], Lamron750, septrinus and 249 guests