I'm trying to click the reset button in Fifa 19 web app with automator.

enter image description here

codes I've tried.

tell application "Safari"
    tell current tab of window 1
        do JavaScript "document.getElementsByClassName('btn-standard')[0].click()"
    end tell
end tell

second one

to clickClassName(theClassName, elementnum)

  tell application "Safari"

      do JavaScript "document.getElementsByClassName('" & theClassName & "')[" & elementnum & "].click();" in current tab of first window

  end tell

end clickClassName
clickClassName("btn-standard", 0)

nothing works any idea why ? I know I can do it with watch me do, but I want to click this button with java script.

After some research it seems that they are more btn-standard tags so is there any way to first select the class="button-container and then the class btn-standard ? Maybe I'm not selecting the button correctly

  • 3
    Possible duplicate of Applescript/Javascript to click submenu item of Jupiter Notebook in Safari(you should be able to use the same method in the answer there) – abc Oct 10 '18 at 0:42
  • I want to do it with javascript I know it can be done with watch me do – Alex Oct 10 '18 at 2:58
  • so you want JavaScript not AppleScript? – abc Oct 10 '18 at 3:28
  • I want to be able to do it in automator with javascript because that's what I need to another button, that button changes the value so it can't be done with normal watch me do, so yea javascript is needed. – Alex Oct 10 '18 at 3:31

This should work for you

tell application "Safari"
    do JavaScript "document.getElementsByClassName('btn-standard')[0].click();" in document 1
end tell

When in doubt, You can always use APPLESCRIPT MAKER Which will create the correct JavaScript for you

I'm not really sure because I never use Safari but maybe enabling "Allow JavaScript from Apple Events" Needs to be enabled?

enter image description here

  • still not working, I know in theory all the example should work, but in practice nothing works, I'll give it a try with apple script maker and see what I find out – Alex Oct 10 '18 at 3:17
  • Which version of macOS are you running? Are you running the code as an applet Or running the code through script editor? – wch1zpink Oct 10 '18 at 3:21
  • running 10.13.6 High Sierra and I'm running the script in an Automator workflow – Alex Oct 10 '18 at 3:25
  • Try running that code snippet in Script Editor app… make sure that Safari page has finished loading before you try running the JavaScript code.. Also check out my updated post about "Allow JavaScript from Apple Events" in Safari – wch1zpink Oct 10 '18 at 3:38
  • yes it is already enabled from Developer Menu, maybe I should try the script in another browser like firefox – Alex Oct 10 '18 at 3:40

You must log in to answer this question.

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