Clicking on JavaScript/jQuery link with Applescript

前端 未结 3 2149
悲哀的现实
悲哀的现实 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 03:54

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

    0 讨论(0)
  • 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.

    0 讨论(0)
  • 2020-12-12 04:19

    I would check out UI Browser it is great for these sort of things, I use it all the time.

    0 讨论(0)
提交回复
热议问题