使用AppleScript单击safari网页按钮

时间:2014-07-23 17:40:24

标签: javascript html safari applescript

我试图找出如何点击网页中的按钮。例如,点击"我感觉幸运"谷歌网页中的按钮。

这就是我的尝试:

tell application "Safari"
    make new document with properties {URL:"https://www.google.com/"}

    do JavaScript "eval(document.getElementById('I'm Feeling Lucky').parentNode.href.substring(11))" in document 1

    delay 1

end tell

但它不起作用。你们中的任何人都知道如何使用applescript点击按钮?

我真的很感谢你的帮助

1 个答案:

答案 0 :(得分:0)

情侣: safari tell块中的最佳做法是告诉文档1.此外,Google上该按钮的ID是错误的。这是页面中的代码:

<button class="gbqfba" aria-label="Google Search" id="gbqfba" name="btnK"><span id="gbqfsa">Google Search</span></button>
<button class="gbqfba" aria-label="I'm Feeling Lucky" onclick="google.x(this,function() {google.ifl &amp;&amp; google.ifl.o();})" id="gbqfbb" name="btnI"><span id="gbqfsb">I'm Feeling Lucky</span></button>

您可以在其中看到两个按钮的ID都是&#39; gbqfba&#39;和&#39; gbqfbb&#39;。但是,如果搜索框中没有输入任何内容(然后更改页面),则单击它们不会执行任何操作,并且通过自动完成我已激活,按钮永远不需要单击。这里是yahoo.com页面的一些代码,它不是动态的。搜索按钮的搜索按钮ID有:id =&#34; search-submit&#34; ......

tell application "Safari"
    activate
    make new document with properties {URL:"https://www.yahoo.com/"}
    delay 2

    tell application "System Events"
        keystroke "stackoverflow"
    end tell
    delay 0.3
    tell document 1
        do JavaScript "document.getElementById('search-submit').click();"
    end tell
end tell

在Chrome中执行相同操作的信息如下: https://stackoverflow.com/a/24644152/3097556