Clicking on JavaScript/jQuery link with Applescript

前端 未结 3 2148
悲哀的现实
悲哀的现实 2020-12-12 03:21

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 implemen

3条回答
  •  有刺的猬
    2020-12-12 04:02

    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.

提交回复
热议问题