Wait for page to finish loading in Chrome, Safari, Firefox (2023)

I just needed such a function for Chrome, tested the .loaded attribute and it seems to work!
Chrome is the simplest of all 3!

Any corrections highly appreciated!!

on waitPageLoaded_Chrome()
	tell application "System Events" to tell application "Google Chrome"
		repeat until (loading of active tab of window 1 is false)
			delay 0.5
		end repeat
	end tell
end waitPageLoaded_Chrome

on waitPageLoaded_Safari()
	tell application "System Events" to tell process "Safari"
		repeat until exists (buttons of groups of toolbar 1 of window 1 whose name = "Reload this page")
			delay 0.5
		end repeat
	end tell
end waitPageLoaded_Safari

on waitPageLoaded_Firefox()
	tell application "System Events" to tell process "Firefox"
		repeat until exists (UI element "Reload" of toolbar "Navigation" of group 1 of window 1)
			delay 0.5
		end repeat
		repeat while (name of UI elements of toolbar "Navigation" of group 1 of window 1 whose description is "Reload") is not {"Reload"}
			delay 0.5
		end repeat
	end tell
end waitPageLoaded_Firefox

Detect finish loading page is a difficult theme. Because web contents are not static now.
They are dynamic and change. Some page change its contents by changing active web contents layer.

What is the main purpose?
URL loading completion?
Page move (URL change)?

Some methods are available for some pages and some other methods does not work with them. And vice versa.

GUI Scripting may one of useful solution. But it does not useful for all pages.

1 Like

Why do you need to tell system events in the Chrome case?