2

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.

enter image description here

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].click()"
  • did you actually see my edit on the last question ( stackoverflow.com/a/21217563/261305 )about how to click the proceed button. Also I have put an answer here that should throw some light on what you are trying to do. – markhunte Jan 30 '14 at 22:06
5

For a minute there I thought you were asking the same question again as last time.

But realise now you want to know how to use Inspect Element to construct your own code.

In the example you give: document.forms[WHATGOESHERE?].click()

The WHATGOESHERE would be the form name.

i.e document.forms['theFormName'].click()

Your website in their wisdom has named the form 'form'

<form method="post" action="/cgi-bin/RNAfold.cgi" enctype="multipart/form-data" name="form"> <input type="hidden" name="PAGE" value="2">

i.e document.forms['form'].click()

But this would not click the input button

You can use the submit() function dot syntax'd on the end :

document.forms['form'].submit()

Safari Applescript : do JavaScript "document.forms['form'].submit()"


Also

In the code I provided you in your last question. I used:

tell document 1

        do JavaScript "document.getElementsByClassName('proceed')[0].click()"

    end tell

This uses the class Name proceed of the forms input element seen here: class="proceed"

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

The getElementsByClassName('proceed')[0] does exactly what it says.

It gets the Elements By ClassName 'proceed'.

The [0] means it will give you the first element in it's results from the Array that would be returned. The array is counted from 0-9. So the first item would be item 0.


A good place to get working examples and information about Javascript is at www.w3schools.com On the front page the have links to their HTML and Javascript examples and Tutorials. The elements of the HTML Dom and Javascript functions are listed for easy access.

3

Despite the fact that this website has shitty code (too few IDs), something like :

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

should work.

It looks for the last element of the first form in the page.


Just so you know :

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

This line does following :

  • It looks in the webpage
  • For a form with id="search"
  • Within that form, it looks for a child node with id=order_list_search_batch.
  • Once this object is found, it performs a click on it.
  • i couldn't get it to work but i've put my code at the bottom of the question. thank you for explaining what that other code was doing! – O.rka Jan 30 '14 at 21:42
  • My bad, I implied you had to add the click() method. I edited my answer ! – Matthieu Riegler Jan 30 '14 at 21:46
  • i don't know why i didn't include .click() in it because I put it on there . is there something going on with my applescript that's not letting it work ? @matthieu riegler – O.rka Jan 30 '14 at 21:57
  • With my new edit it should work. – Matthieu Riegler Jan 30 '14 at 22:03

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .