使用AppleScript点击Safari浏览器网页按钮 [英] Click on safari web page button using AppleScript

查看:1838
本文介绍了使用AppleScript点击Safari浏览器网页按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图弄清楚如何点击网页上的按钮。例如,点击google网页中的我很幸运按钮。



这是我试过的:

 告诉应用程序Safari
使用属性创建新文档{URL:https://www.google.com/}
$ b在文档1中,$ b做JavaScripteval(document.getElementById('I feel feel lucky')。parentNode.href.substring(11))

延迟1

结束告诉

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



我会非常感谢您的帮助

解决方案


safari tell block的最佳做法是告诉文档1.此外,Google上该按钮的ID是错误的。以下是该页面的代码:

 < button class =gbqfbaaria-label =Google Searchid = gbqfbaname =btnK>< span id =gbqfsa> Google搜寻< / span>< / button> 

您可以看到两个按钮的ID都是'gbqfba'和'gbqfbb'。但是,如果在搜索框中没有输入任何内容,点击它们将不会执行任何操作,而搜索框会改变页面,并且通过自动完成功能来激活,按钮永远不需要点击。以下是yahoo.com页面的一些不动态代码。搜索按钮的搜索按钮ID有:id =search-submit...

$ p $ tell applicationSafari
激活
使用属性创建新文档{URL:https://www.yahoo.com/}
延迟2

告诉应用程序系统事件
keystrokestackoverflow
end tell
delay 0.3
tell document 1
do JavaScriptdocument.getElementById('search-submit')。click();
end tell
end tell


https://stackoverflow.com/a/24644152/3097556


I'm trying to figure out how to click on the button in a webpage. For example click on the "I'm Feeling Lucky" button in google web page.

This is what I have tried:

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

But it doesn't work. Any of you knows how can I click on the button using applescript?

I'll really appreciate your help

解决方案

Couple things: Best practice in the safari tell block is to also tell document 1. Also, the ID for that button on Google is wrong. Here's the code from the page:

<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>

where you can see the ID's for both buttons are 'gbqfba' and 'gbqfbb'. However, clicking them does nothing if nothing is typed in the search box, which then alters the page, and with auto-complete that I have active, the button never needs click. Here's some code for the yahoo.com page which is not dynamic. The search button ID for the search button there is: 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

Info on doing the same in Chrome is here: https://stackoverflow.com/a/24644152/3097556

这篇关于使用AppleScript点击Safari浏览器网页按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆