【问题标题】:Click on safari web page button using AppleScript使用 AppleScript 单击 Safari 网页按钮
【发布时间】:2014-09-15 00:55:46
【问题描述】:

我试图弄清楚如何点击网页中的按钮。例如点击谷歌网页中的“手气不错”按钮。

这是我尝试过的:

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单击按钮吗?

非常感谢您的帮助

【问题讨论】:

    标签: javascript html safari applescript


    【解决方案1】:

    几件事: 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 是“gbqfba”和“gbqfbb”。但是,如果在搜索框中没有输入任何内容,则单击它们不会执行任何操作,然后会更改页面,并且通过我激活的自动完成功能,该按钮永远不需要单击。这是 yahoo.com 页面的一些代码,它不是动态的。搜索按钮的搜索按钮 ID 为:id="search-submit" ...

    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

    【讨论】:

      猜你喜欢
      • 2015-11-17
      • 1970-01-01
      • 2017-03-22
      • 2013-03-11
      • 1970-01-01
      • 1970-01-01
      • 2021-12-16
      • 2017-06-07
      • 1970-01-01
      相关资源
      最近更新 更多