点击与AppleScript的的JavaScript / jQuery的链接 [英] Clicking on JavaScript/jQuery link with Applescript
问题描述
我没有做过太多的AppleScript用,但我想这个过程自动化。我有一个需要点击的按钮的网站。然而,按钮使用JavaScript / jQuery的/ AJAX实现像这样:
I haven't ever done much with AppleScript, but I'm trying to automate this process. I have a website with a Button that needs to be clicked. However the button is implemented with JavaScript/jQuery/AJAX like so:
<div id="divIdOfButton" class="someClass">
<div class="divClassOfButton someOtherClass">
<img src="imgOfButton">
<div>
...
<script type="text/javascript">
$(document).ready(function() {
$('#divIdOfButton .divClassOfButton).click(function() {
...
}}
我想这没有任何运气
I tried this without any luck
tell application "Safari"
activate
delay 1
set URL of first document to "http://example.com/"
do JavaScript "document.getElementById('divIdOfButton').getElementByClassName('divClassOfButton')[0].click()" in front document
end tell
我做了一堆搜寻,但没有找到任何东西。真的AP preciate一些帮助。
I did a bunch of searching, but couldn't find anything. Would really appreciate some help.
推荐答案
大多数浏览器会忽略到单击事件处理程序直接调用,它似乎(显然出于安全原因 - 不要让我开始在浏览器中的JavaScript安全模式),所以你的点击()
通话少了点什么都没有。您可以通过JavaScript的事件调度机制触发click事件(见<一href=\"http://stackoverflow.com/questions/9603288/missing-value-errors-when-heavily-using-javascript-in-safari-by-applescript\">this问题和矿的答案)。但是,如果你已经针对该网站确实包含了jQuery,所有你需要做的是:
Most browsers will ignore direct calls to the click event handler, it seems (apparently for security reasons – don’t get me started on the JavaScript security model in browsers), so your click()
call just does nothing at all. You can trigger the click event via JavaScript’s event dispatching mechanism (see this question and answer of mine). However, if the site you target already does include jQuery, all you need to do is:
tell application "Safari"
do JavaScript "$('#divIdOfButton .divClassOfButton').click();" in front document
end tell
如果有在课堂上的几个按钮你的 DIV
,你需要添加一个过滤器前pression,即
If there are several buttons of the class in your DIV
, you’ll need to add a filter expression, i.e.
tell application "Safari"
do JavaScript "$('#divIdOfButton .divClassOfButton :equ(0)).click();" in front document
end tell
但你会失去 querySelectorAll
的性能优势没有这个的jQuery杠杆(见的 jQuery的API文档)。
but you will lose the performance advantage of querySelectorAll
leveraged by jQuery without this (see jQuery API docs).
的按触发在Safari堆栈溢出站点收件箱下拉测试。的
这篇关于点击与AppleScript的的JavaScript / jQuery的链接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!