How to properly check if Safari tab has finished loading?

Hello Keyboard maestros,

Looking for a way to properly reload a Safari tab and check if a page has finished loading using AppleScript + Javascript.

Seems like the build in action "for waiting for safari to finish loading" doesn't work.

This script should work but it doesn't, it shows the page has completed loading while it keeps loading.

tell application "Safari"
	set sameURL to URL of tab 2 of front window
	set URL of tab 2 of front window to sameURL
end tell

set t to ""
tell application "Safari" to tell tab 2 of window 1
	repeat until t is "complete"
		set t to do JavaScript "document.readyState"
	end repeat
end tell

There is a page load indicator in the inspect page mode, is there a way to use it to understand if the page is still loading or not?

Not sure how to access it.

Screenshot 2020-04-13 at 01.09.20

The only solution that I could manage and which actually works is to refresh the page and run JS periodically to check if the page contains some text or an element - That means the page has finished loading.

after 12 month and 10 different ways - the easiest (of course) came to my mind and it works perfectly

just put in a "PAUSE UNTIL" and then select "MENU CONDITON" the menu item "Stopp" (*) is NOT ENABLED
for a clean run I always add an addiontional PAUSE of 0.2 to 0.5 after it

works 100% and safe

2 Likes

How to check if the menu item "Stop" is disabled using AppleScript only without using the built in KM actions?

Thanks in advance.

what Browser do you use?

@Primus_Interpares

im interested in your method you posted but not sure i full follow

im using safari and following you ended with this currently

what is the menu item "Stopp" (*) you refer to?

thx a lot!

Z

Sorry that was someone elses macro
stop is what is appropriate for you
see other options under pause until
and you can also try
wait until safari is finished loading..

I am using Safari

Sadly, this no longer works. With an update to Safari about a year ago the Stop menu now is always enabled. For the Wait For Front Browser to Finish Loading action, Keyboard Maestro checks the Stop menu (but no longer in Safari since it is no longer useful), and then document.readyState is “complete”. It continuously checks this until it remains “complete” for the specified time in the action.

anyway there is a regarding menutopic for each of the common
browsers:

Safari: Sichern unter is DURING the loading NOT ENABLED

1 Like

I have found the most reliable way is to pause until a specific HTML element is found on the page.
This works for Safari, Google Chrome, and all of the Chrome-based browsers.
See:

MACRO: Pause Until Web Page Element Exists [Example]

2 Likes

The macro provided by JMichaelTX solves the question.

Also, repeatedly checking if a web page contains a particular element solves the question as well.