为什么不;t'执行javascript'当在浏览器中键入相同的代码时,Applescript中的调用会执

why doesn't 'do javascript' call in Applescript execute when the same code typed into browser does?

本文关键字:代码 调用 Applescript javascript 执行 为什么不 浏览器      更新时间:2023-09-26

我正试图弄清楚,当在Safari位置栏中键入相同的javascript代码时,为什么我的Applescript什么都不做。

转到搜索结果页面,例如:http://www.google.com/search?q=test。要获得正确的行为,请在位置栏中键入此信息,然后点击回车键:

javascript: document.getElementsByClassName('vspib')[0].click();

您会看到它为第一个搜索结果选择了放大镜。

这就是我想要通过javascript实现的。所以我输入了以下内容:

tell application "Safari"
    activate
    do JavaScript "document.getElementsByClassName('vspib')[0].click();" in document 1
end tell

然而,它什么也没做。有什么想法吗?

问题是do JavaScript必须正确处理Safari窗口中的选项卡。

如果搜索结果页面是Safari最前面窗口中的当前选项卡,则以下脚本对我有效:

tell application "Safari"
    activate
    set theScript to "document.getElementsByClassName('vspib')[0].click();"
    do JavaScript theScript in current tab of first window
end tell