Applescript click on a button


Is this possible to click on the button "detailed" using AppleScript (Google chrome)


I would usually use this for a click


but in this situation I'm not sure how to proceed

update : I updated my reply as I didn't realised the code was hided by askdifferent text editor

that what I usually use :

tell application "Google Chrome"
    tell tab 4 of window 1 to set myClickingbutton to execute javascript "document.getElementById('Clasenamehere').click();
end tell

Best Answer

So you already know the JavaScript method for clicking on an HTML element using its class name as a selector:


where [0] denoted the first item in the array of elements, all of whom share the same classname. Often, it's better--at least in my mind—to try and use an element's id, which provides a unique attribute by which to select an element:


To run this code from Google Chrome by way of AppleScript, the general form looks like this:

    set js to "document.getElementsByClassName('classnamehere')[0].click();"

    tell application "Google Chrome" to tell ¬
        the front window to tell ¬
        the active tab to ¬
        set jsResult to execute javascript js

The important feature about execute javascript is that it must be directed to a specific tab in Google Chrome. You can't send the command to a window or the application object. So:

    tell application "Google Chrome" to ¬
        execute javascript js

will throw an error (even on the assumption that you had defined js).

The syntax can also take this form:

    tell application "Google Chrome" to tell front window to ¬
        set jsResult to execute of tab 1 javascript js

or this form:

    tell application "Google Chrome" to tell front window to ¬
        (execute javascript js) in front tab

In each of these commands, the javascript is being told to execute in a specific tab.

You haven't yet specified what "detailed" refers to, and when you do, I can update this answer accordingly. Working on the assumption that it's either a class name or an id of an HTML element, you can use one of the expressions at the top and plug it straight into the code examples I gave you. So, you might use the JavaScript method getElementsByClassName, which would take this form:


where [0] could end up being changed to [1], [2], etc. depending how many other elements have this class name. What I would then do is define my AppleScript variable js to hold the JavaScript command for me:

    set js to "document.getElementsByClassName('detailed')[0].click();"

then tell the active tab in Chrome to execute it:

    tell application "Google Chrome"
        tell active tab of window 1 to ¬
            execute javascript js
    end tell

The execute command will always return a value, even if it's just an empty string. Therefore, if you wish, you can assign that value to a variable in AppleScript to use later in your script:

    set jsResult to the result

Or, more directly:

    set jsResult to execute javascript js