Hi,
I want to create a little app in Safari that can automatically click on links in Safari in a certain order over and over. I need it in order to test out a website I have created. What language/code should I use? How can I learn it?
THANK YOU!!!
Hi Andrew -
This is certainly possible, although not necessarily easy, but I've come up with two ways to do it. One is with Automator, using it's built-in Safari support. It's pretty limited however, and basically all it does is open each link on whatever page is visible in a new Safari window.
A second but more complicated method uses JavaScript and AppleScript. It ain't pretty, but it might be more suitable to what you want. However, it requires that every link in your page (i.e. the anchor tags) have a "name" associated with them, for example name="link1". Then you can use AppleScript to request Safari to execute a JavaScript code, for example:
tell application "Safari"
do JavaScript "some_java_script_function();" in the first document
end tell
To get you started I've uploaded a zipfile to my website at the following URL:
http://www.bgstech.com/download.php?AutomatingSafari.zip
This file contains an example workflow (double-clicking it should launch Automator for you), a simple web page with 4 links in it, and an AppleScript/JavaScript hybrid to open each link, pause 10 seconds, and then revert back.
The workflow should show the limitations of that method, while the "LinkTester.scpt" should show more possibilities.
If you're going to go the latter route, I'd recommend reading up on the JavaScript Document Object Model (DOM), there are plenty of sites such as
www.w3schools.com and
krook.org, as well as AppleScript itself, the primary resource being
Apple's developer site.
Best of luck!
- Brian
--------
Brian S.
MacOSX.com Technical Support
brian@macosx.com
WOW, THANK YOU SO MUCH!!!
There is one thing I left out though. What is the javascript code that will make it automatically choose a bubble (the one's that are round and when clicked on create a little dot). Here is what I need done in the exact order:
1. Click on a certain button.
2. Give 2-3 seconds for page to load.
3. Select a certain bubble.
4. Click on a certain button.
5. Give 2-3 seconds for page to load
6. repeat steps 1-5 continuously.
If you could create an outline so I could just fill in the blanks that would be SUPER AWESOME. The buttons I have do not have the link names that you have described but I know the beginning of the url (for example:
www.andrew.com/mac/........). The thing is that the .... after
www.andrew.com/mac/ is totally random and changes all the time. Another problem that may occur is when the buttons are clicked on, it may come up with an error message on the next page. If you could make it automatically go to the previous page and refresh, THAT WOULD BE WAY COOL!!!
-THANK YOU SO MUCH
-Andrew
Hi Andrew -
Well, it sounds as though you need to select buttons on a form as opposed to clicking specific links on the page. The same overall method can be applied here (AppleScript's "do JavaScript" call combined with the correct Javascript code), but of course the Javascript would be different.
If you would care to re-download the "AutomatingSafari.zip" file, I've added a "FormTester.scpt" AppleScript which shows the basics of selecting form items with javascript. Basically, you can select a radiobutton on a form with something like "document.form01.result[0].click();", and then simulate clicking on the Submit button with something like "document.form01.submit();".
Unfortunately I'm not in a position to write a full custom Applescript/Javascript application for you to just fill in the blanks, but I hope I've given you enough of a start so that you can continue. Between the links I provided for Javascript DOM specification, and Apple's documentation on AppleScript, you should be able to find everything you need.
Best of luck!
- Brian
--------
Brian S.
MacOSX.com Technical Support
brian@macosx.com