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

Helpful script writing tricks and HowTo's
User avatar
eblanc
Posts: 71
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: 1426
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: 71
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: 47
Joined: 04 Mar 2019, 13:58
Contact:

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!
Les medecins choisissent viagrasansordonnancefr.com sans danger pour la sante
User avatar
eblanc
Posts: 71
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.
GratefulJim
Posts: 4
Joined: 04 Nov 2019, 14:10

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

04 Nov 2019, 14:27

This is a good tutorial ... but I am looking for something specific.

I am trying to determine if a specific web page is running in a tab in chrome.
I'm doing this as part of a AHK script to load my WAMP development environment all in one shot.
1) Loads the apache/MySql server manager console ( working )
2) Loads the web / code authoring tool ( Dreamweaver ) .. ( Working )
3) If chrome is not running, loads chrome. Then loads specific localhost web pages into separate tabs. ( Working ).

The final stage I want to complete is to be able to determine if any of the pages i loaded into new tabs is not running.. and simply reopen that tab page.
This will allow me to simply hit my key combo for the AHK script and load any page whose tab was accidentally closed or closed legitimately .. but i need to reopen.

The following is my script:

Code: Select all

#NoEnv  ; Recommended for performance and compatibility with future AutoHotkey releases.
#Warn  ; Enable warnings to assist with detecting common errors.
SendMode Input  ; Recommended for new scripts due to its superior speed and reliability.
SetWorkingDir %A_ScriptDir%  ; Ensures a consistent starting directory.

; -----------------------------------------------------------------------------------------
; Perform the script functions once the (Windows Key + z) key combination is pressed.
#z::

If !WinExist("ahk_exe manager-windows.exe")  ; If the server manager isnt running..   RUN IT. ...(Equivalent to IfWinNotExist)
{
	Run, "K:\WAMP\manager-windows.exe"
	
	; Wait until we can detect the program is running - FORMAT:(WinWait, useProcessName, ProcessName, , Timeout in seconds).
	WinWait, ahk_exe manager-windows.exe, , 5
	if ErrorLevel
	{
		MsgBox, WinWait timed out opening WAMP Server control program !
		return
	}
}


; Open up the web authoring / coding program you want to use.
If !WinExist("ahk_exe Dreamweaver.exe")  ; If the web authoring / coding program isnt running..   RUN IT.
{
	Run, "C:\Program Files\Adobe\Adobe Dreamweaver CC 2019\Dreamweaver.exe"
	
	; Wait until we can detect the program is running - FORMAT:(WinWait, useProcessName, ProcessName, , Timeout in seconds).
	WinWait, ahk_exe Dreamweaver.exe, , 5
	if ErrorLevel
	{
		MsgBox, WinWait timed out opening Dreamweaver !
		return
	}
}


; Open up the browser with the tabs for the admin pages.
If !WinExist("ahk_exe chrome.exe")  ; If the browser isnt running..   RUN IT.
{
	Run, "C:\Program Files (x86)\Google\Chrome\Application\chrome.exe"
	
	; Wait until we can detect the program is running - FORMAT:(WinWait, useProcessName, ProcessName, , Timeout in seconds).
	WinWait, ahk_exe chrome.exe, , 5
	if ErrorLevel
	{
		MsgBox, WinWait timed out opening Chrome !
		return
	}
	else
	{		
	; Load the admin pages to access in separate new tabs.
	Run, "C:\Program Files (x86)\Google\Chrome\Application\chrome.exe" "http localhost /phpmyadmin/"  Broken Link for safety
	Run, "C:\Program Files (x86)\Google\Chrome\Application\chrome.exe" "http localhost /Applications//phpMyEdit/"  Broken Link for safety
	Run, "C:\Program Files (x86)\Google\Chrome\Application\chrome.exe" "http localhost /Applications/LuxCal/"  Broken Link for safety
	Run, "C:\Program Files (x86)\Google\Chrome\Application\chrome.exe" "http localhost /phpbb/"  Broken Link for safety
	}
}
else 
{
	; Chrome is already running..  so just open the admin pages to access in separate new tabs.
	Run, "C:\Program Files (x86)\Google\Chrome\Application\chrome.exe" "http localhost /phpmyadmin/"  Broken Link for safety
	Run, "C:\Program Files (x86)\Google\Chrome\Application\chrome.exe" "http localhost /Applications//phpMyEdit/"  Broken Link for safety
	Run, "C:\Program Files (x86)\Google\Chrome\Application\chrome.exe" "http localhost /Applications/LuxCal/"  Broken Link for safety
	Run, "C:\Program Files (x86)\Google\Chrome\Application\chrome.exe" "http localhost /phpbb/"  Broken Link for safety
}
return
; -----------------------------------------------------------------------------------------
User avatar
eblanc
Posts: 71
Joined: 08 May 2019, 14:41

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

04 Nov 2019, 15:13

GratefulJim wrote:
04 Nov 2019, 14:27
This is a good tutorial ... but I am looking for something specific.

I am trying to determine if a specific web page is running in a tab in chrome.
I'm doing this as part of a AHK script to load my WAMP development environment all in one shot.
1) Loads the apache/MySql server manager console ( working )
2) Loads the web / code authoring tool ( Dreamweaver ) .. ( Working )
3) If chrome is not running, loads chrome. Then loads specific localhost web pages into separate tabs. ( Working ).

The final stage I want to complete is to be able to determine if any of the pages i loaded into new tabs is not running.. and simply reopen that tab page.
This will allow me to simply hit my key combo for the AHK script and load any page whose tab was accidentally closed or closed legitimately .. but i need to reopen.

The following is my script:

Code: Select all

#NoEnv  ; Recommended for performance and compatibility with future AutoHotkey releases.
#Warn  ; Enable warnings to assist with detecting common errors.
SendMode Input  ; Recommended for new scripts due to its superior speed and reliability.
SetWorkingDir %A_ScriptDir%  ; Ensures a consistent starting directory.

; -----------------------------------------------------------------------------------------
; Perform the script functions once the (Windows Key + z) key combination is pressed.
#z::

If !WinExist("ahk_exe manager-windows.exe")  ; If the server manager isnt running..   RUN IT. ...(Equivalent to IfWinNotExist)
{
	Run, "K:\WAMP\manager-windows.exe"
	
	; Wait until we can detect the program is running - FORMAT:(WinWait, useProcessName, ProcessName, , Timeout in seconds).
	WinWait, ahk_exe manager-windows.exe, , 5
	if ErrorLevel
	{
		MsgBox, WinWait timed out opening WAMP Server control program !
		return
	}
}


; Open up the web authoring / coding program you want to use.
If !WinExist("ahk_exe Dreamweaver.exe")  ; If the web authoring / coding program isnt running..   RUN IT.
{
	Run, "C:\Program Files\Adobe\Adobe Dreamweaver CC 2019\Dreamweaver.exe"
	
	; Wait until we can detect the program is running - FORMAT:(WinWait, useProcessName, ProcessName, , Timeout in seconds).
	WinWait, ahk_exe Dreamweaver.exe, , 5
	if ErrorLevel
	{
		MsgBox, WinWait timed out opening Dreamweaver !
		return
	}
}


; Open up the browser with the tabs for the admin pages.
If !WinExist("ahk_exe chrome.exe")  ; If the browser isnt running..   RUN IT.
{
	Run, "C:\Program Files (x86)\Google\Chrome\Application\chrome.exe"
	
	; Wait until we can detect the program is running - FORMAT:(WinWait, useProcessName, ProcessName, , Timeout in seconds).
	WinWait, ahk_exe chrome.exe, , 5
	if ErrorLevel
	{
		MsgBox, WinWait timed out opening Chrome !
		return
	}
	else
	{		
	; Load the admin pages to access in separate new tabs.
	Run, "C:\Program Files (x86)\Google\Chrome\Application\chrome.exe" "http localhost /phpmyadmin/"  Broken Link for safety
	Run, "C:\Program Files (x86)\Google\Chrome\Application\chrome.exe" "http localhost /Applications//phpMyEdit/"  Broken Link for safety
	Run, "C:\Program Files (x86)\Google\Chrome\Application\chrome.exe" "http localhost /Applications/LuxCal/"  Broken Link for safety
	Run, "C:\Program Files (x86)\Google\Chrome\Application\chrome.exe" "http localhost /phpbb/"  Broken Link for safety
	}
}
else 
{
	; Chrome is already running..  so just open the admin pages to access in separate new tabs.
	Run, "C:\Program Files (x86)\Google\Chrome\Application\chrome.exe" "http localhost /phpmyadmin/"  Broken Link for safety
	Run, "C:\Program Files (x86)\Google\Chrome\Application\chrome.exe" "http localhost /Applications//phpMyEdit/"  Broken Link for safety
	Run, "C:\Program Files (x86)\Google\Chrome\Application\chrome.exe" "http localhost /Applications/LuxCal/"  Broken Link for safety
	Run, "C:\Program Files (x86)\Google\Chrome\Application\chrome.exe" "http localhost /phpbb/"  Broken Link for safety
}
return
; -----------------------------------------------------------------------------------------
I'm not seeing where you're introducing the getpixelcolor command. So I'm not sure how to respond.

I would maybe recommend if the tabs are set in a specific way that they're always there, to find 1 pixel that is consistent, probably a piece of the logo that shows up in the tab.

If you have multiple tabs, then Image search might work better, although it's finicky and never actually gotten it to work properly.

Chrome doesn't support COM well, because if you were using IE or a different browser with better COM options you might be able to search for activity not based on a picture but actually more so in the program itself.

Let me know if this helps.
SOTE
Posts: 1426
Joined: 15 Jun 2015, 06:21

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

07 Nov 2019, 21:10

eblanc wrote:
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 !
I think people can have issues with imagesearch, because they aren't using some of the options possible.

ImageSearch, X, Y, 0, 0, A_ScreenWidth, A_ScreenHeight, *12 C:\loaded.png For example, prior to putting the info of the file to be used, you can select the color variation allowed (*12 C:\loaded.png). This can help in the search.

https://www.autohotkey.com/docs/commands/ImageSearch.htm

Playing with CoordMode can also help. A person might try CoordMode Pixel, where coordinates are relative to the screen, and not the active Window. This affects not only ImageSearch, but also PixelGetColor.

https://www.autohotkey.com/docs/commands/CoordMode.htm

Code: Select all

#SingleInstance Force
CoordMode, Pixel, Screen

^a::

Send, {f5}
pageloaded := false
while(pageloaded = false)
{
	sleep 1000
	ImageSearch, X, Y, 0, 0, A_ScreenWidth, A_ScreenHeight, *12 C:\loaded.png
	if ErrorLevel = 0
	{
		pageloaded := true
	}
}
MsgBox, Web paged loaded
return
You can also search for multiple colors, instead of one color with PixelGetColor.

Code: Select all

#SingleInstance force

LFColors :="
(
0xED1B24
0x23B14D
0x3F47CC
0xFEF200
0x000000
0xFFFFFF
)"

X1 := 0, Y1 := 0, X2 := 1920, Y2 := 1080

Loop, Parse, LFColors, `n
{	
	PixelSearch, X, Y, X1, Y1, X2, Y2, A_LoopField, 0, RGB Fast
	Msgbox % A_LoopField
	If (!ErrorLevel)
	{	
		Msgbox,,, Found Something!
	} 
	Else
	{	
		Msgbox,,, Found Nothing!
	}
} 
Last edited by SOTE on 07 Nov 2019, 22:21, edited 1 time in total.
SOTE
Posts: 1426
Joined: 15 Jun 2015, 06:21

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

07 Nov 2019, 21:45

GratefulJim wrote:
04 Nov 2019, 14:27
This is a good tutorial ... but I am looking for something specific.

I am trying to determine if a specific web page is running in a tab in chrome.
Your question might be more suited to be in the AutoHotkey Help Forum. However, you can use WindowSpy.ahk (in the directory of where you have AutoHotkey.exe installed). You can then use WindowSpy.ahk to verify the information that the tab displays. A tab in Chrome will show up like a Window Title. You can have AutoHotkey get the tab info from Chrome using WinGetTitle.

https://www.autohotkey.com/docs/commands/WinGetTitle.htm

Code: Select all

SetTitleMatchMode, 2
SetTitleMatchMode, slow

TabTitle := "AutoHotkey - Google Search"

WinActivate, ahk_exe chrome.exe
Sleep 1000
WinGetTitle, WinTitle, A
If InStr(WinTitle, TabTitle)
{
	MsgBox, I See You
}	
Else
{
	MsgBox, I Don't See Anything
}
You can then combine such code with PixelGetColor or ImageSearch to more clearly identify the web page or have code do something to it.
axtonboyer
Posts: 1
Joined: 05 Dec 2019, 11:32

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

13 Dec 2019, 10:42

I was looking for the same. Let me try them
prism17
Posts: 2
Joined: 06 Jan 2020, 02:07

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

06 Jan 2020, 02:11

hi dude, maybe Python is a better choice, haha.
User avatar
eblanc
Posts: 71
Joined: 08 May 2019, 14:41

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

08 Jan 2020, 15:49

prism17 wrote:
06 Jan 2020, 02:11
hi dude, maybe Python is a better choice, haha.
Python is not as user-friendly as AHK for beginner coders.
User avatar
tdalon
Posts: 44
Joined: 21 Apr 2017, 07:19
Location: Germany
Contact:

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

04 Aug 2023, 03:35

See Descolada's UIA_Browser Library https://github.com/Descolada/UIAutomation/blob/main/Lib/UIA_Browser.ahk especially the Navigate and WaitPageLoad functions.

It looks for the properties of the Refresh button / no image search.
rockitdontstopit
Posts: 176
Joined: 12 Nov 2022, 15:47

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

11 Aug 2023, 14:35

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!
To see if something has finished loading or appears, I used to use ImageSearch. But I think I was cropping my images too large.

Switched to PixelGetColor. Pretty reliable and the fasted "image search" since you're looking for just one pixel. However the location of that pixel must be constant. If you widen the search area too much you may find the same pixel color somewhere else.

Now I use images that are about 3x3, 4x4, or 6x3 pixels. Screenshot, paste into MS Paint, Max zoom to 800%, and precisely crop image. I think it's better to avoid shadowing, like on text images. The key is to find a unique image that is as small as possible. With a very small image, searching an entire 1080p screen is almost instant. I tested this by making like a 3x3 image of my desktop recycle bin. The small image was found almost instantly. ImageSearch searches from upper left to bottom right I believe. You're not limited to a small search area like in PixelGetColor.

Another thing I've found useful in some cases is to see if a pixel or image NO LONGER appears.
It was the best of times, it was the worst of times...

Return to “Tutorials (v1)”

Who is online

Users browsing this forum: No registered users and 34 guests