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.

user500119
  • 13
  • 1
  • 4

3 Answers3

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.

Community
  • 1
  • 1
kopischke
  • 3,393
  • 1
  • 21
  • 40
  • 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](http://www.w3.org/TR/html5/editing.html#dom-click) (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 May 01 '12 at 06:04
  • @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. – kopischke May 01 '12 at 16:02
  • 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*). – kopischke May 01 '12 at 18:03
  • 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` – roberthuttinger Feb 25 '16 at 14:27
1

I would check out UI Browser it is great for these sort of things, I use it all the time.

kosa
  • 65,990
  • 13
  • 130
  • 167
Skillet
  • 11
  • 1
-1

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

Niet the Dark Absol
  • 320,036
  • 81
  • 464
  • 592
  • Anyway, on a reasonably current browser, you’d be much better off using [`querySelectorAll`](http://www.w3.org/TR/selectors-api/), which is blazing fast and uses CSS selector strings *à la* jQuery. But that is not the issue – see [my answer](http://stackoverflow.com/a/10143157/990363) for details. – kopischke Apr 13 '12 at 14:56
  • Agree with kopischke there - especially since `querySelector[All]` is supported in more browsers than `getElementsByClassName`. – Niet the Dark Absol Apr 13 '12 at 14:59
  • this is a comment not an answer – roberthuttinger Feb 25 '16 at 14:24