Clicking on JavaScriptjQuery link with Applescript - Stack Overflow

I haven't ever done much with AppleScript, but I'm trying to automate this process. I have a

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 "/"
    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.

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./"
    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.

Share Improve this question edited Apr 13, 2012 at 14:26 user500119 asked Apr 13, 2012 at 13:34 user500119user500119 131 silver badge4 bronze badges 4
  • Unfortunately it's a site that requires a membership.. – user500119 Commented Apr 13, 2012 at 14:10
  • Would it work if I copied the whole $(document.ready(.... into applescript and said "do Javascript " ... " in front document? – user500119 Commented Apr 13, 2012 at 14:12
  • Just covering the bases here, but you have been wrapping that do JavaScript statement in a tell application "Safari" block, yes? – kopischke Commented Apr 13, 2012 at 14:20
  • Yes, I did. I added the full code above – user500119 Commented Apr 13, 2012 at 14:26
Add a ment  | 

2 Answers 2

Reset to default 5

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.

I'm not an Applescript user, but I can tell you right away that getElementByClassName should be getElementsByClassName - "elements" in the plural.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745131577a4612998.html

相关推荐

  • Clicking on JavaScriptjQuery link with Applescript - Stack Overflow

    I haven't ever done much with AppleScript, but I'm trying to automate this process. I have a

    13小时前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信