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

Helpful script writing tricks and HowTo's
User avatar
eblanc
Posts: 65
Joined: 08 May 2019, 14:41

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

19 Jun 2019, 14:11

Hi Everyone, this took me a while to find, And I wanted to share.

My main script was intended to check if a page was loaded and if so to start the script. Instead of doing things manually or putting a Sleep timer that could be inaccurate or too time-consuming.


I am using pixelgetcolor to check if a specific pixel on the screen has changed color.

For example, if my script needed the Google page to be loaded before inputting a script I would run something like this.

Code: Select all

!z::
;Is Google Page Loaded
loop
{ 
	 sleep, 500
	 PixelGetColor, pColor, 852,366, RGB
     If (pColor = "0x4285F4")
		{
		msgbox, I found it %pcolor%
		break
		}
		
}
;The following is an example of what the next script could be ;)
send, Los Parkinson Aromas del ayer{enter}
return
Here I am checking to see if a blue pixel of the first "G" of Google is showing up. And testing for the color, which is blue. (But more exactly it is 0x4285F4)

Everybody's application will be different. so you'll have to source the pixel location and the color.

The first is fairly easy, you need window Spy (Right-click on your active AHK script in the desktop bar)
Click on the window you are looking at (so it's active)
Go to the pixel you want to select by putting your mouse over
Write down the window value.

Next, we should run a script that tests the color in that pixel. Even better here's one that tests both location and color

Code: Select all

!q::
;Testing page
msgbox, start
mousemove, 852, 366
PixelGetColor, pColor, 852,366, RGB
Msgbox, Pixel color %pcolor% 
return
Write down the color. make sure the mouse moved to the correct position and that should be all the information you need.

Couple things to keep in mind:

1. Some buttons when the mouse is floating over them, will in fact change color. so you may not want' to use mouse move, or you may want to add extra mouse move away from the button
2. Remember that I am using RGB values, not sure why it wouldn't be but I was not able to run the script without the RGB value
3. It is possible to add a timer (tickount command), or so that if the page doesn't load, it exists, or closes the window, etc..
4. An easier way to calculate a timer would be to add a sleep into the loop, let's say 1000 (1 second) and chose the number of loops. ie Loop, 30. Which would mean the script would be looking for the page for 30 seconds then break automatically.
5. If the script is running it will always bee looking for that pixel, and it won't stop until it does, because it's on an infinite loop.

Hope that helps someobody. Shoot me questions if you have them.
Last edited by eblanc on 20 Jun 2019, 11:35, edited 1 time in total.
SOTE
Posts: 736
Joined: 15 Jun 2015, 06:21

Re: How to Check if Webpage is Loaded (Google Chrome, Firefox, Internet Explorer)

19 Jun 2019, 21:54

A related method is to also use ImageSearch. I mention this, because it's probably a good idea for people to know this method too for achieving the same result, and it gives yet another option.

Demonstration video https://youtu.be/64lMvLAZBFg (autohotkey wait for page to load)

Note- video quality is a little bit poor. Also, you need to have a image file for the below script to work.

Code: Select all

^a::

Send, {f5}
pageloaded := false
while(pageloaded = false)
{
	sleep 1000
	ImageSearch, X, Y, 0, 0, A_ScreenWidth, A_ScreenHeight, loaded.png
	if ErrorLevel = 0
	{
		pageloaded := true
	}
}
MsgBox, Web paged loaded
return
User avatar
eblanc
Posts: 65
Joined: 08 May 2019, 14:41

Re: How to Check if Webpage is Loaded (Google Chrome, Firefox, Internet Explorer)

20 Jun 2019, 11:28

That is a good point @SOTE, I didn't mention it because it never worked for me. It also gets a little bit more complicated. Cropping image + calculating search box, having the picture with the same folder, slow processing if a big search box, and just plainly more difficult to get it to work. in the end, I opted for this option because it's really simple, all it takes is knowing which pixel changes and to what color once the page is loaded.

But this is definitely a good mention for someone who may need an application that needs to search for a full image. I still think pixelgetcolor is a better method because it's more efficient and easier to calculate. I haven't found yet and application where Imagesearch would be better.

Thanks for adding to the thread @SOTE !
Vaklev
Posts: 43
Joined: 04 Mar 2019, 13:58

Re: How to Check if Webpage is Loaded (Google Chrome, Firefox, Internet Explorer)

14 Aug 2019, 12:15

I've been using image recognition and doing a search for a small portion of the screen, works perfectly fine for me and the pixel get color never worked as good, I guess everyone's application methods are different, we have to use whatever works!
User avatar
eblanc
Posts: 65
Joined: 08 May 2019, 14:41

Re: How to Check if Webpage is Loaded (Google Chrome, Firefox, Internet Explorer)

13 Sep 2019, 17:45

Vaklev wrote:
14 Aug 2019, 12:15
I've been using image recognition and doing a search for a small portion of the screen, works perfectly fine for me and the pixel get color never worked as good, I guess everyone's application methods are different, we have to use whatever works!
Glad it help somebody. It took me a while to build this, reading other people's ideas etc... Thanks for the comment, I am glad this post wasn't lost in the ether.

Return to “Tutorials”

Who is online

Users browsing this forum: No registered users and 9 guests