最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

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

matteradmin8PV0评论

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
Post a comment

comment list (0)

  1. No comments so far