admin管理员组文章数量:1335832
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
3 Answers
Reset to default 3I 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
版权声明:本文标题:javascript - Why does my getElementById( ) result with "missing value" in Applescript? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742388780a2465570.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论