You can also click elements with JavaScript:
tell application "Google Chrome" to tell active tab of window 1
execute javascript "document.getElementById('chrome-web-store-title').click()"
--execute javascript "document.querySelectorAll('.tile-grid .most-visited')[3].click()"
end tell
Focusing an existing tab by URL:
tell application "Google Chrome"
repeat with w in windows
set i to 1
repeat with t in tabs of w
if URL of t starts with "https://mail.google" then
set active tab index of w to i
set index of w to 1
return
end if
set i to i + 1
end repeat
end repeat
end tell
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="pointer"" 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.
Best Answer
So you already know the JavaScript method for clicking on an HTML element using its class name as a selector:
where
[0]
denoted the first item in the array of elements, all of whom share the same classname. Often, it's better--at least in my mind—to try and use an element'sid
, which provides a unique attribute by which to select an element:To run this code from Google Chrome by way of AppleScript, the general form looks like this:
The important feature about
execute javascript
is that it must be directed to a specifictab
in Google Chrome. You can't send the command to awindow
or theapplication
object. So:will throw an error (even on the assumption that you had defined
js
).The syntax can also take this form:
or this form:
In each of these commands, the
javascript
is being told to execute in a specifictab
.You haven't yet specified what "detailed" refers to, and when you do, I can update this answer accordingly. Working on the assumption that it's either a
class
name or anid
of an HTML element, you can use one of the expressions at the top and plug it straight into the code examples I gave you. So, you might use the JavaScript methodgetElementsByClassName
, which would take this form:where
[0]
could end up being changed to[1]
,[2]
, etc. depending how many other elements have this class name. What I would then do is define my AppleScript variablejs
to hold the JavaScript command for me:then tell the
active tab
in Chrome to execute it:The
execute
command will always return a value, even if it's just an empty string. Therefore, if you wish, you can assign that value to a variable in AppleScript to use later in your script:Or, more directly: