That is funny, I was actually working on that a few weeks ago…. NOTE: This will not work if it is already inside of a Safari Tell Statement. To utilize clicking by tag name just paste the following into your AppleScript:
#Applescript documentation code#
This code searches the document for any elements, like, , or anything else inside of, and then tells Safari to click it. ToclicktagName (thetagName, elementnum ) tell application "Safari"ĭo JavaScript "document.getElementsByTagName('" & thetagName & "').click() " in document 1 To click on the tag do the same as above for Name and Class, however use the following slightly altered code: The only thing that we have to identify what we need to click on here is what is called the Tag. Lets take the above example of clicking on the search button again… clickName("btnG", 0) -would click the first element with name "btnG"ĬlickName("btnG", 1) -would click the second element with name "btnG"ĬlickName("btnG", 2) -would click the third element with name "btnG"įor name, you will most likey only need to use clickName(“theName”, 0)… but when trying to click on something by Class or by Tag you will have to play around with what number works… Clicking an HTML Element by Class with AppleScriptĬlicking an element by class is the same as clicking an element by name in practice. Because of this we have to tell Applescript which element with the name “btnG” we want to click on. When trying to identify an element by name or class we have to specify which instance on the page we are talking about.įor example… There could be 10 search buttons on the page, and they could all have the name=”btnG” inside of them. This makes it easy for us to identify elements with ids. That is because on a web page there can only be 1 specific element with 1 specific ID. You might have noticed that this code, clickName(“btnG”,0), has a little more to it than the previous clickID statement. Now, usually this will work, especially if there is only 1 thing on the page with the name=”btnG”, however if there are multiple elements with name=”btnG” you will have to do a bit more work. Now when you would like to click on something on a web page, like the search button on Google, you can insert this anywhere in your code. To use this info to click that search button type this into the top of your AppleScript code… toclickName (theName, elementnum ) tell application "Safari"ĭo JavaScript "document.getElementsByName('" & theName & "').click() " in document 1 It looks like we found something that says name=”btnG”… Lets inspect the element just like before to find out what we need to do next! For this example we are looking to click the search button on Google. Lets say that there is nothing that says “id”, the next best thing is to find a something that says “name”. clickID(" htb_more")Ĭlicking an HTML Element by Name with AppleScript Now when you would like to click on something on a web page, lets say its the “More” button on Google, you can insert this anywhere in your code. The code to find an element by ID and then click on it is as follows:įirst paste this into the top of your AppleScript document toclickID (theId ) -creates a function that we can use over and over again instead of writing this code over and over again tell application "Safari" - lets AppleScript know what program to controllĭo JavaScript "document.getElementById('" & theId & "').click() " in document 1 - performs JavaScript code that clicks on the element of a specific idĮnd tell - tells Applescript you are done talking to Safari endclickID - lets AppleScript know we are done with the function In this case there is something that says, id =”htb_more” Clicking an HTML Element by ID with AppleScript The best case scenario that you can hope for is if inside of the element there is something that says “id”. If none of these things exist, don’t worry, there is one last thing you can try. Now you have to inspect the code… we are looking for something that says either id, name, or class. This will bring up a box at the bottom of the screen with a whole bunch of code. Using Chrome, right click on the element that you are trying to click using applescript. In this case, we will take a look at a search for sporks on Google. Our goal is to click the “More” button on the menu. I find that these are the easiest to use when trying to inspect different elements on a web page. Step 1: Figure out which method you need to use…įirst you have to open up another browser like Firefox or Chrome. Which method you choose depends on how the code of the webpage you are working with. There are a few different ways you can make Apple script click a button in Safari.
#Applescript documentation how to#
Now that you know how to make Safari visit a web page using AppleScript, you will probably want to take your automation to the next level by being able to interact with that page! For this tutorial I am going to teach you how automate clicking on almost anything on almost any web page.