August 4, 2023

AutoHotkey: How to wait for Browser page to be loaded

In the-automator Newsletter and recent YouTube channel, a way to wait for a Browser to be loaded was shared.
After some research, I share here a better way to wait for a browser page to be loaded. 

Problem description

For many automation task with the browser, you want to be able to wait till a page/url is loaded in the Browser, avoiding a fixed paused i.e. pausing only the minimum necessary time.

Benchmark

There are different solutions available in the net to implement this. Some using Image/Pixel Search.

Best Solution

The best solution - but that was not posted as answer in the forums is to use the UIA.

It is implemented in Descolada's UIA_Browser library:


You can load an url with the option to wait untill the page is loaded using the function Navigate or call the WaitPageLoad method separately.

Navigate(url, targetTitle="", waitLoadTimeOut=-1, sleepAfter=500)
Navigates to URL and waits page to load

WaitPageLoad(targetTitle="", timeOut=-1, sleepAfter=500, titleMatchMode=3, titleCaseSensitive=True) 
Waits the browser page to load to targetTitle, default timeOut is indefinite waiting, sleepAfter additionally sleeps for 200ms after the page has loaded. 

The implementation checks that the refresh button has changed to the load button.

(The solution shared by the-Automator is far from the best. I can't understand why the comment I have posted on their the YouTube video was deleted.)

See also

Checking if page has loaded when you don't have access to the DOM - YouTube

How do I wait for Google Chrome to load a webpage before continuing in AutoHotkey? - Stack Overflow

How to Check if Webpage is Loaded (Google Chrome, Firefox, Internet Explorer) - AutoHotkey Community

UIAutomation/Lib/UIA_Browser.ahk at main · Descolada/UIAutomation

No comments:

Post a Comment