Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does my getElementById( ) result with "missing value" in Applescript?

My goal is to determine whether or not an element is on a page and perform an action on that element, if it exists.

In this code, I'm only showing the second part of trying to perform an action on an element.

My code is:

tell application "Safari" 
    open location "http://www.apple.com" 
    tell front document 
        do JavaScript "document.getElementById(\"gn-store\").click()" 
    end tell 
end tell 

However, I always get a result of "missing value", from this code and similar things I've tried (including calling a function in Js, to return a value from getElementById( ).

I don't understand what I'm doing wrong. I've been at it for hours. (and for the record, I am an applescript noob).

like image 744
Stack Johan Avatar asked Oct 21 '22 00:10

Stack Johan


1 Answers

I find Chrome reacts better to Javascript via AppleScript...

tell application "Google Chrome"
    if not (exists window 1) then reopen
    activate
    tell window 1 to tell active tab
        set its URL to "http://www.apple.com/"
        delay 3
        execute javascript "document.getElementById('gn-store').childNodes[0].click()"
    end tell
end tell
like image 199
adayzdone Avatar answered Oct 27 '22 09:10

adayzdone