1

I haven't ever done much with AppleScript, but I'm trying to automate this process. I have a website with a Button that needs to be clicked. However the button is implemented with JavaScript/jQuery/AJAX like so:

<div id="divIdOfButton" class="someClass">
    <div class="divClassOfButton someOtherClass">
        <img src="imgOfButton">
    <div>

...

<script type="text/javascript">

$(document).ready(function() {
$('#divIdOfButton .divClassOfButton).click(function() {
...
}}

I tried this without any luck

tell application "Safari"
    activate
    delay 1
    set URL of first document to "http://example.com/"
    do JavaScript "document.getElementById('divIdOfButton').getElementByClassName('divClassOfButton')[0].click()" in front document
end tell

I did a bunch of searching, but couldn't find anything. Would really appreciate some help.

4
  • Unfortunately it's a site that requires a membership.. Commented Apr 13, 2012 at 14:10
  • Would it work if I copied the whole $(document.ready(.... into applescript and said "do Javascript " ... " in front document? Commented Apr 13, 2012 at 14:12
  • Just covering the bases here, but you have been wrapping that do JavaScript statement in a tell application "Safari" block, yes? Commented Apr 13, 2012 at 14:20
  • Yes, I did. I added the full code above Commented Apr 13, 2012 at 14:26

2 Answers 2

5

Most browsers will ignore direct calls to the click event handler, it seems (apparently for security reasons – don’t get me started on the JavaScript security model in browsers), so your click() call just does nothing at all. You can trigger the click event via JavaScript’s event dispatching mechanism (see this question and answer of mine). However, if the site you target already does include jQuery, all you need to do is:

tell application "Safari"
    do JavaScript "$('#divIdOfButton .divClassOfButton').click();" in front document
end tell

If there are several buttons of the class in your DIV, you’ll need to add a filter expression, i.e.

tell application "Safari"
    do JavaScript "$('#divIdOfButton .divClassOfButton :equ(0)).click();" in front document
end tell

but you will lose the performance advantage of querySelectorAll leveraged by jQuery without this (see jQuery API docs).

Tested by triggering the inbox dropdown on Stack Overflow sites in Safari.

Sign up to request clarification or add additional context in comments.

4 Comments

The statement You cannot trigger the click event from plain JavaScript is incorrect (and contradicted by your link). You can use dispatchEvent (W3C compliant) or fireEvent (IE compatible) as appropriate, or if the browser supports HTML5, call the click method (e.g. element.click()). The behaviour is not particularly consistent (e.g. some versions of Firefox will not follow synthetic clicks on links), but if the OP is only concerned about a particular browser, it may be fine.
@RobG true, the sentence is not exact enough – “you cannot call the click handler directly in plain JavaScript in most browsers” would be closer to the mark (edited as such). As to HTML5 support (re-)instating this ability, however, this seems untrue at least of current versions of Safari on OS X, which in my experimentation ignored direct calls to the click handler.
Added another iteration on the introductory sentence in the hope of achieving more precise semantics. Also added a reference to the click event dispatch approach (aka simulate.js).
do JavaScript only works in Safari. If you need to do something javascript inside Google Chrome, this works: tell application "Google Chrome" activate execute front window's active tab javascript "if (window.console) console.log( 'hi' );" end tell
0

I'm not an Applescript user, but I can tell you right away that getElementByClassName should be getElementsByClassName - "elements" in the plural.

3 Comments

Anyway, on a reasonably current browser, you’d be much better off using querySelectorAll, which is blazing fast and uses CSS selector strings à la jQuery. But that is not the issue – see my answer for details.
Agree with kopischke there - especially since querySelector[All] is supported in more browsers than getElementsByClassName.
this is a comment not an answer

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.