How to Get AppleScript to Wait for a Page to Load

NEW! AppleScript Maker Beta: Hey Everyone, I am working on a new AppleScript tool that is going to BLOW YOUR MIND! After reading this tutorial use this tool to easily build your AppleScripts. Click Here!

In previous tutorials we covered  how to make applescript open a web page, how to use AppleScript to fill out forms on a web page, and how to click buttons on web pages with AppleScript, and How to Extract information from a website using AppleScript.

When trying these scripts you might run into an issue where a page takes a long time to load. you could always use something like the Delay function… but that doesn’t always work as the loading time might be longer than your delay or it may make your scripts run longer than they have to if the page loads fast.

What you need to do is create a script that waits for an element on the page to load and then continues with your script.

***Make sure you pick an element that is unique to the page if you can. That way it does not pull the info from the old page and trigger your script to continue.***

For example… lets say there is an element on the page that returns a number. We would set a test variable to 0, and try and pull the element over and over again until the 0 changes.

This should do the trick… copy and paste this into the top of your text:

to waitForLoadbyClass(theClass, num)
 
set tester to 0
 
repeat 60 times
 
try
tell application "Safari"
set tester to do JavaScript "
document.getElementsByClassName('" & theClass & "')[" & num & "].innerHTML;" in document 1
end tell
 
end try
 
if tester > 0 then
exit repeat
end if
 
delay 1
 
end repeat
 
 
end waitForLoadbyClass 

This particular function uses Class. So you would need to get the class name of the element you are trying to load.

theClass is where you put the class name.

num is where you would put the instance number of the class name.

Use waitForLoadbyClass(theClass, num) where you want call the function.

If this is not making sense or if you would like to change this script to pull something like ID, Tag, or Name… please read How to Extract information from a website using AppleScript which goes into much more detail of the main part of this code.

 

 

 

2 Responses to “How to Get AppleScript to Wait for a Page to Load

  • Hello samuel,
    i’ve tried something with your code and with an id element, seems to be working fine, i’m really a noob though so it might be useless but working for some totally diferent reason. What do you think about it ?

    to waitForLoadbyid(theid)
    set tester to “theid”
    repeat 5 times
    try
    tell application “Safari”
    set tester to do JavaScript ”
    document.getElementsByID(‘” & “theid” & “‘).innerHTML;” in document 1
    end tell
    end try
    if tester is true then
    exit repeat
    end if
    delay 1
    end repeat
    end waitForLoadbyid

    thanks for the great tutos, i’ve learn a lot!

    • It looks great to me! If it works go with it. Let me know if you need any help with any of your future projects.

Leave a Reply

Your email address will not be published. Required fields are marked *