为什么我的getElementById()在Applescript中导致"缺失值"?

Sta*_*han 5 javascript applescript getelementbyid

我的目标是确定元素是否在页面上并对该元素执行操作(如果存在).

在这段代码中,我只展示了尝试对元素执行操作的第二部分.

我的代码是:

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

但是,我总是得到一个"缺失值"的结果,从这个代码和我尝试过的类似的事情(包括在Js中调用一个函数,从getElementById()返回一个值.

我不明白我做错了什么.我已经好几个小时了.(并且为了记录,我是一个苹果菜菜鸟).

ada*_*one 3

我发现 Chrome 通过 AppleScript 对 Javascript 的反应更好...

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
Run Code Online (Sandbox Code Playgroud)

  • “childNodes”部分成功了!我使用了原来的代码,添加了 3 秒。在“打开位置”之后延迟,在“执行 Javascript”之后,我在“执行 javascript”之后复制粘贴了您的代码 - 工作得很好。多谢! (2认同)