我想在谷歌浏览器的所有标签上运行以下AppleScript。
tell application "Google Chrome" to tell active tab of window 1
execute javascript "document.getElementsByClassName('bt bt3 bw4')[0].click();"
end tell
我设法使用Safari在所有选项卡上运行它;
tell application "Safari"
repeat with t in tabs of windows
do JavaScript "document.getElementsByClassName('bt bt3 bw4')[0].click();" in t
end repeat
end tell
但是无法在Chrome中复制它。
任何人有任何建议或知道是否可以实现?
谢谢
由于您没有提供用于测试getElementsByClassName('bt bt3 bw4')
的URL,所以我已经打开了几个Google Chrome windows,每个都有几个tabs,其中带有URL] >并以Ask Question button为目标,其class name为ws-nowrap s-btn s-btn__primary
。
以下示例
AppleScript 代码对我有用,因为它在每个Google]的每个tab上单击每个问问题 按钮 Chrome 窗口:。tell application "Google Chrome" repeat with t in tabs of windows tell t to execute javascript "document.getElementsByClassName('ws-nowrap s-btn s-btn__primary')[0].click();" end repeat end tell
提示提示:将鼠标悬停在水平滚动上可以查看完整的代码
AppleScript code中,用ws-nowrap s-btn s-btn__primary
替换[class name bt bt3 bw4
,在此Google Chrome中,< code的[version(如果它在code的Safari version中起作用)。 注意:
•如果有多个object
具有与'bt bt3 bw4
相同的class name,则如果getElementsByClassName()
HTML DOM方法需要具有正确的index value不是[0]
。如果不是您提供的常量value,则可能需要其他编码才能以编程方式确定正确的value。 •示例
AppleScript 代码就是这样,并且不包含任何错误处理。用户有责任添加任何适当的,需要的或想要的。看看try中的error statement和AppleScript Language Guide statement。另请参见Working with Errors。