1

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

for="7cd6f428-1are-41f2-9054-c452d7fe242-view-selector-e5f5de50-634a-45bc-bf30-98e2a64676cc">Detailed

I would usually use this for a click

 "document.getElementsByName('clasenamehere')[0].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
3
  • Could you provide a sample of the entire script?
    – drivec
    Commented Mar 23, 2018 at 15:02
  • execute front window's tab 1 javascript "document.getElementsByName('namehere')[0].click();"
    – Kevin
    Commented Mar 23, 2018 at 15:08
  • What is the URL of the page you are trying to action the button on, and which button is it? Commented Mar 23, 2018 at 16:38

1 Answer 1

2

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

document.getElementsByClassName('classnamehere')[0].click();

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:

document.getElementById('idnamehere').click();

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:

document.getElementsByClassName('detailed')[0].click();

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
5
  • Thank you for the detailed answer, I updated my question because I didn't realised that the button name was hidden, however I approved your answer as I think there is no solution to my request due to the part encrypted name and also , even if my usually click snipped work, your seems way more clean and appropriate
    – Kevin
    Commented Mar 24, 2018 at 13:52
  • Thanks. If you're willing to share the URL, I could take a look and see if there's a way to do it.
    – CJK
    Commented Mar 24, 2018 at 14:03
  • Is there any code to just hover/move the mouse to that html element instead of clicking? I really need a way to just move the mouse to the html element instead of clicking! Thanks.
    – Zui Zui
    Commented Jun 11, 2020 at 5:14
  • @DuyDuy You need to post that as a question on this site. But what do you wish to achieve by hovering/moving the mouse over an html element ? That's the first thing that needs to be known in order to be possible to help.
    – CJK
    Commented Jun 15, 2020 at 0:49
  • Dear @CJK I want to download some images which their url only appear when I hover my mouse over some buttons. (i.e. shopee.ph/… notice the "Black" variation of the product is disable and can't be clicked, when you hover your mouse over, the image appears). I've made one with Keyboard Maestro but it's slow. It would be perfect if I can make it with Java but I know nothing about Java, please give some advices, I don't even know where to paste your script above. Thx alot
    – Zui Zui
    Commented Jun 16, 2020 at 13:53

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .