单击带有 Applescript 的 JavaScript/jQuery 链接 [英] Clicking on JavaScript/jQuery link with Applescript

查看:23
本文介绍了单击带有 Applescript 的 JavaScript/jQuery 链接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我从来没有用 AppleScript 做过很多事情,但我正在尝试自动化这个过程.我有一个带有需要单击的按钮的网站.然而,按钮是用 JavaScript/jQuery/AJAX 实现的,如下所示:

<div class="divClassOfButton someOtherClass"><img src="imgOfButton"><div>...<script type="text/javascript">$(document).ready(function() {$('#divIdOfButton .divClassOfButton).click(function() {...}}

我没有运气就试过了

告诉应用程序Safari"启用延迟 1将第一个文档的 URL 设置为http://example.com/"在前端文档中执行 JavaScript "document.getElementById('divIdOfButton').getElementByClassName('divClassOfButton')[0].click()"结束告诉

我进行了大量搜索,但找不到任何东西.真的很感激一些帮助.

解决方案

大多数浏览器会忽略对点击事件处理程序的直接调用,它似乎(显然是出于安全原因——不要让我开始研究浏览器中的 JavaScript 安全模型),所以您的 click() 调用根本没有任何作用.您可以通过 JavaScript 的事件调度机制触发点击事件(参见 这个问题我的答案).但是,如果您定位的站点已经包含 jQuery,那么您需要做的就是:

告诉应用程序Safari"做 JavaScript "$('#divIdOfButton .divClassOfButton').click();"在前面的文件结束告诉

如果您的DIV中有多个类的按钮,则需要添加过滤表达式,即

告诉应用程序Safari"做 JavaScript "$('#divIdOfButton .divClassOfButton :equ(0)).click();"在前面的文件结束告诉

但是如果没有这个,您将失去 jQuery 利用的 querySelectorAll 的性能优势(请参阅 jQuery API 文档).

通过触发 Safari 中 Stack Overflow 网站上的收件箱下拉菜单进行测试.

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

I did a bunch of searching, but couldn't find anything. Would really appreciate some help.

解决方案

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

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

but you will lose the performance advantage of querySelectorAll leveraged by jQuery without this (see jQuery API docs).

Tested by triggering the inbox dropdown on Stack Overflow sites in Safari.

这篇关于单击带有 Applescript 的 JavaScript/jQuery 链接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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