使用Javascript | Applescript在Safari中单击按钮

时间:2014-01-30 21:45:33

标签: javascript html macos click applescript

我意识到有几个关于此的线索,它们都非常特定于某个网站。我的背景是Python,而不是Javascript或Applescript,我对如何实现此操作感到困惑。

我看过几个脚本正在执行此操作,即:

tell application "Safari"

activate

do JavaScript "document.forms['search']['order_list_search_batch'].click()"

end tell

采取行动的最佳方法是什么?

我对"document.forms[WHATGOESHERE?].click()"

之间的内容感到困惑

我正在尝试点击http://rna.tbi.univie.ac.at/cgi-bin/RNAfold.cgi上的继续按钮。

我在Proceed按钮上找到了“Inspect Element”并得到了这段代码:

<input style="cursor: pointer;" value="" name="proceed" class="proceed" onmouseover="this.style.cursor=&quot;pointer&quot;" type="submit">

如何根据Inspect Element结果知道在脚本中放置什么单击此按钮?我想了解所以我可以在多个案例中使用此方法。它没有一个href链接。

enter image description here

当前代码无效

tell application "Safari" to activate
open location "http://rna.tbi.univie.ac.at/cgi-bin/RNAfold.cgi"
delay 3
tell application "Safari" to do JavaScript "document.forms[0].elements[document.forms[0].elements.length-1]"

2 个答案:

答案 0 :(得分:3)

更改您的JavaScript查询
document.forms['search']['order_list_search_batch'].click()

document.forms['form']['proceed'].click()

如果您检查“继续”按钮,则会发现它位于带有&#34;名称&#34;的表单元素中。属性值为#34;形式&#34;。按钮本身有一个&#34;名称&#34;属性值为#34;继续&#34;。因此,在您的JavaScript中,括号中的第一个术语将查找名为&#34; form&#34;的表单。第二个是指名为&#34的元素;继续&#34;在&#34;形式&#34; -form中。如果这是有道理的; - )

此外,您必须告诉AppleScript执行JavaScript的文档。如果没有其他标签打开,您刚刚打开的文档可能是文档1。

希望这有帮助!

整个代码:

tell application "Safari"
activate
open location "http://rna.tbi.univie.ac.at/cgi-bin/RNAfold.cgi"
delay 3
do JavaScript "document.forms['form']['proceed'].click()" in document 1
end tell

答案 1 :(得分:1)

你可以使用javascript dom函数getElementsByName(),它会返回一个包含该名称的所有元素的数组,你可以(在这种情况下)定位第一个项目。

此外,您还需要专门定位要执行javascript的标签,以使其在Safari中正常运行。

见下文

tell application "Safari"
    activate
    open location "http://rna.tbi.univie.ac.at/cgi-bin/RNAfold.cgi"
    delay 3
    do JavaScript "document.getElementsByName('proceed')[0].click();" in current tab of first window
end tell