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.

 

 

 

delta

6 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.

  • Hey there..
    I am trying to scrape some text from a webpage using Google Chrome..

    tell application “Google Chrome”
    activate
    set thescript to “document.getElementsById(‘loginform:xtAlert_body’).innerHTML;”
    set myvar to execute javascript thescript
    display dialog myvar
    end tell

    This always returns “msng”. Even though the html element contains the following:

    some text
    • sam@cubemg.com
      6 years ago

      Try getting the element by class instead of id. That might do the trick.

      • still does not work. I tested this on http://www.google.de but I get the same “msng” content. Even with Safari it does not work:

        tell application “Google Chrome”
        activate
        set thescript to “document.getElementByClassName(‘_Vbu’).innerHTML”
        set myvar to execute active tab of front window javascript thescript
        display dialog myvar
        end tell

        This script should return “Deutschland”, the country name of the german google website.

        • I got it going using [0] in conjunction with the classname:

          tell application “Google Chrome”
          activate
          set thescript to “document.getElementsByClassName(‘_Vbu’)[0].innerHTML;”
          set myvar to execute active tab of front window javascript thescript
          display dialog myvar
          end tell

Leave a Reply to Guidok Cancel reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.