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 atell 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
2 Answers
Reset to default 5Most 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
评论列表(0条)