1
votes

I am trying to click a simple button in safari when on a web page using applescript. I have the following code:

tell application "Safari"
    activate
    repeat 5 times
        do JavaScript "document.all(\"Roll Again\").click()" in document 1
        delay 2
    end repeat
end tell

However, when I run this code, I get an error: "missing value". The event log displays the following result:

tell application "Safari"
    activate
    do JavaScript "document.all(\"Roll Again\").click()" in document 1
        --> missing value
    do JavaScript "document.all(\"Roll Again\").click()" in document 1
        --> missing value
    do JavaScript "document.all(\"Roll Again\").click()" in document 1
        --> missing value
    do JavaScript "document.all(\"Roll Again\").click()" in document 1
        --> missing value
    do JavaScript "document.all(\"Roll Again\").click()" in document 1
        --> missing value
end tell

Could someone please tell me why the button is not being pressed and what missing value means? Is there another simpler way to achieve the button press? I can press the button using automator, however I would like to do it using AppleScript.

P.S. document is the current window in safari

1
Please include the URL. - adayzdone

1 Answers

0
votes

document.all is specific to IE and doesn't work in Safari. Try using getElementById or querySelectorAll:

tell application "Safari"
    activate
    tell document 1
        do JavaScript "document.getElementById(\"nav-badges\").click()"
        delay 5
        do JavaScript "document.querySelectorAll(\"#hmenus a\")[0].click()"
    end tell
end tell