A Look at Techniques for Ensuring a Web Page Fully Loads Before Continuing an AutoHotkey Script
When writing an AutoHotkey script which uses the Web, I rarely open a browser anymore. I either download the source code to a file (URLDownLoadToFile command) or a variable (Example: Download text to a variable technique). That means I don’t need to wait for a Web page to load into a browser—although as expressed earlier similar issues exist.
One of the most common reasons for requiring a fully loaded Web page involves AutoHotkey auto-logon scripts which insert usernames and passwords before continuing. If the page download hesitates, the script outruns the Web process and sends the data to an empty browser window. Most of the Web download problems brought to my attention by AutoHotkey users relate to auto-login scripts.
The Web Page Download Problem
Initial connections (and reloads) to Web pages remain infamously unpredictable. Even after a browser connects to the Web server, the Web page can take a considerable amount of time to fully download. The WinWait command only ensures the existence of the browser window, not the completion of the Web page display.
When accessing a Web page, after startup, the browser first contacts the Web page server. That remote Web system eventually acknowledges contact and responds with a handshake. Then, depending upon the speed of the connection (and the whims of the Web server), the browser begins to receive and decode the page. On occasion, the process may hesitate or hang up. This causes tremendous variability in the time it takes for any Web site to settle into the browser. Unfortunately, since the server doesn’t tell the browser when it’s done, it’s not easy to detect when the machine finishes sending the code. In AutoHotkey, you’ll find a number of alternative methods for dealing with this Web page loading problem—none of them perfect.
Pause (Sleep) Until Loaded
One of the most common and simplest solutions cited by AutoHotkey users adds the Sleep command to the script forcing it to pause long enough to allow the page to top off. I’ve seen some fixes which stop a script for up to 10 seconds. Even then, it doesn’t work every time.
With Internet Explorer Use DllCall and COM
When using Internet Explorer (or presumably Microsoft Edge), you’ll find techniques specifically available for the Microsoft browser. It looks a little involved (and I’ve never tried it) but, if it works, you have a reason to switch to a Microsoft browser for these special Web applications. If you use another default browser, you can force Microsoft Edge to run:
If you really need a reliable method for determining when a Web page wraps up the loading process, then you may want to shift to a Microsoft product.
Wait for the Status Bar, a Color, an Image, or Text to Appear in the Page
Another common approach for detecting Web page loading involves searching for something embedded in the Web page. The status bar, a color pixel, image, or text in the page can act as a cue once the page appears in the browser window. For the image and text detection, you must identify the target data in advance. These approaches may offer some of the more reliable techniques since the page must expose itself before AutoHotkey can detect any of these items.
For the image detection technique, functions use the ImageSearch command:
ImageSearch, OutputVarX, OutputVarY, X1, Y1, X2, Y2, ImageFile
which looks for a pre-cropped section of an image toward the bottom of a known Web page (as an example, check out Xeo786’s response in this forum post). This works for regularly accessed Web pages but won’t help with random browsing.
Use the Cursor Status
Another technique advocated by some users checks the AutoHotkey A_Cursor variable which contains the current cursor type. The cursor usually turns into a spinning circle (AppStarting) when loading a program. This may work with many Web browsers:
run, http://www.computoredge.com/ tooltip, Web page loading! Sleep, 200 while (A_Cursor = "AppStarting" or A_Cursor = "Wait") ; Wait for browser continue Sleep, 200 while (A_Cursor = "AppStarting" or A_Cursor = "Wait") ; Wait for browser continue tooltip MsgBox Done loading!
This example runs the loop twice to increase accuracy, but I found the technique only marginally reliable.
Add a Message Box to Pause the Script
With the exception of the Sleep command, all of the above techniques carry too much complication for me. If I load a Web page into a browser, I’m usually sitting at my computer watching. In that case, I would use a message box which pauses the script until ready for the next step—ensuring the Web page completely loads.
MsgBox, 4096,, Continue after page loads!
The 4096 option ensures that the MsgBox sits always-on-top until dismissed. Then the processing continues.
I recommend rethinking your use of a Web browser in your AutoHotkey scripts—except when loading a page only for viewing and access. If you write an application which draws data from the page, then using techniques which download the source code, then extract data (as in the case of the SynonymLookup.ahk script) may make more sense. But in applications requiring inputting text into a Web page and/or clicking buttons and settings, you may have no other option than to guess at the right length of time for the Sleep command or the addition of MsgBox commands.
You may find this blog unsatisfying as an answer for the browser Web page download problem (I do), but I found no simple, reliable solution. If someone has any better answers, then I’m all ears.
This post was proofread by Grammarly
(Any other mistakes are all mine.)
(Full disclosure: If you sign up for a free Grammarly account, I get 20¢. I use the spelling/grammar checking service all the time, but, then again, I write a lot more than most people. I recommend Grammarly because it works and it’s free.)