html - Using Javascript | Applescript to click button in Safari - Stack Overflow

I realize there are several threads about this and they are all very specific to certain website . My b

I realize there are several threads about this and they are all very specific to certain website . My background is in Python, not Javascript or Applescript, and I'm confused on exactly how to achieve this action .

I've seen several scripts doing this action, namely:

tell application "Safari"

activate

do JavaScript "document.forms['search']['order_list_search_batch'].click()"

end tell

What is the best method to act this out ?

i'm confused on what goes in between "document.forms[WHATGOESHERE?].click()"

I'm trying to click the Proceed button on .cgi.

I went to "Inspect Element" on the Proceed button and got this code:

<input style="cursor: pointer;" value="" name="proceed" class="proceed" onmouseover="this.style.cursor=&quot;pointer&quot;" type="submit">

How do I know what to put in script to click this button based off of the Inspect Element results ? I want to understand so I can use this method in more than one case. There's not a href link that it goes to.

Current code is not working

tell application "Safari" to activate
open location ".cgi"
delay 3
tell application "Safari" to do JavaScript "document.forms[0].elements[document.forms[0].elements.length-1]"

I realize there are several threads about this and they are all very specific to certain website . My background is in Python, not Javascript or Applescript, and I'm confused on exactly how to achieve this action .

I've seen several scripts doing this action, namely:

tell application "Safari"

activate

do JavaScript "document.forms['search']['order_list_search_batch'].click()"

end tell

What is the best method to act this out ?

i'm confused on what goes in between "document.forms[WHATGOESHERE?].click()"

I'm trying to click the Proceed button on http://rna.tbi.univie.ac.at/cgi-bin/RNAfold.cgi.

I went to "Inspect Element" on the Proceed button and got this code:

<input style="cursor: pointer;" value="" name="proceed" class="proceed" onmouseover="this.style.cursor=&quot;pointer&quot;" type="submit">

How do I know what to put in script to click this button based off of the Inspect Element results ? I want to understand so I can use this method in more than one case. There's not a href link that it goes to.

Current code is not working

tell application "Safari" to activate
open location "http://rna.tbi.univie.ac.at/cgi-bin/RNAfold.cgi"
delay 3
tell application "Safari" to do JavaScript "document.forms[0].elements[document.forms[0].elements.length-1]"
Share Improve this question asked Jan 30, 2014 at 21:45 O.rkaO.rka 30.8k78 gold badges213 silver badges335 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 3

Change your JavaScript query from

document.forms['search']['order_list_search_batch'].click()

to

document.forms['form']['proceed'].click()

If you inspect the proceed button, you'll find that it's inside a form element with a "name" attribute with the value "form". The button itself has a "name" attribute with the value "proceed". So in your JavaScript, the first term between brackets looks for the form named "form" and the second one refers to the element named "proceed" inside the "form"-form. If that makes any sense ;-)

Also, you will have to tell AppleScript in which document to execute the JavaScript. The document you just opened will probably be document 1, if there are no other tabs open.

Hope this helps!

The entire code:

tell application "Safari"
activate
open location "http://rna.tbi.univie.ac.at/cgi-bin/RNAfold.cgi"
delay 3
do JavaScript "document.forms['form']['proceed'].click()" in document 1
end tell

You could use the javascript dom function getElementsByName(), it will return an array of all elements with that name and you can (in this case) target the first item.

Also you will need to specifically target the tab you want to execute the javascript in to get it working in Safari.

See below

tell application "Safari"
    activate
    open location "http://rna.tbi.univie.ac.at/cgi-bin/RNAfold.cgi"
    delay 3
    do JavaScript "document.getElementsByName('proceed')[0].click();" in current tab of first window
end tell

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

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

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

关注微信