javascript - Why does my getElementById( ) result with "missing value" in Applescript? - Stack Overflow

My goal is to determine whether or not an element is on a page and perform an action on that element, i

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 "" 
    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).

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." 
    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).

Share Improve this question asked Sep 26, 2013 at 2:13 Stack JohanStack Johan 3791 gold badge6 silver badges24 bronze badges 1
  • This will probably look terrible in mini-Markdown formatting, but tis work - checks for an element, then clicks if it exists: tell application "Safari" open location "apple." delay 3 tell front document set appearance to do JavaScript "document.contains(document.getElementById('gn-store'))" end tell if appearance is true then tell front document do JavaScript "document.getElementById('gn-store').childNodes[0].click()" end tell end tell – Stack Johan Commented Sep 26, 2013 at 4:34
Add a ment  | 

3 Answers 3

Reset to default 3

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./"
        delay 3
        execute javascript "document.getElementById('gn-store').childNodes[0].click()"
    end tell
end tell

You are likely trying to get the element before it exists (the page just started loading). Try this:

do JavaScript "window.onload=function(){document.getElementById(\"gn-store\").click()}" 

There is a way in Applescript to click this button. It involves GUI Scripting, which can be enabled by checking the 'Enable Access for Assistive Devices' checkbox in the Accessibility pane of System Preferences (as shown).

You can click 'Store' on the Apple site by using

tell application "Safari"
    open
    activate
    set frontmost to true
    open location "http://www.apple."
end tell
tell application "System Events" to tell process "Safari"
    delay 3
    tell UI element 2 of group 1 of UI element 1 of scroll area of group 1 of group 1 of group 2 of window "Apple" to perform action "AXPress"
end tell

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1742388781a4434594.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信