So, on the browser page with the item you’re trying to access, try this:
Right-click (or control-click) on the item you want to access. Select “inspect” (or “Inspect Element”).
A new pane will open in your safari window (if it’s not already there) and the “Elements” tab will be active, and some portion of the HTML will be selected.
Copy that text.
Run the first script below in your script editor.
A Choose from List dialog will appear and show you the options for addressing that element.
By ID is preferred. If you use name, class name, etc. you’ll also need to provide a number. (The default is 0 for the first instance)
Select the ones you want to try, and paste them into the second script below (after the use statement).
That has the appropriate JavaScript handlers to click an element.
Uncomment handler calls for the method(s) you selected and pasted in above.
See if that works. (If you want to do more than click the element I may have a handler for that too.
use AppleScript version "2.4" -- Yosemite (10.10) or later
use scripting additions
set elementStuff to the clipboard
set clipboardText to ParseElementInspector(elementStuff)
set userSelection to choose from list clipboardText ¬
with title ("Element Name, Id or Class") ¬
with prompt ("Sected variable(s) for this element to copy/paste into your script") ¬
default items 1 ¬
OK button name ¬
"Okay" cancel button name ¬
"Cancel" multiple selections allowed true ¬
with empty selection allowed
if userSelection is not in {false, {}} then
set saveTID to AppleScript's text item delimiters
set AppleScript's text item delimiters to {return & return}
set clipboardText to clipboardText as text
set AppleScript's text item delimiters to saveTID
set the clipboard to clipboardText
end if
on ParseElementInspector(elementInfo)
set elementInfoText to {}
set saveTID to AppleScript's text item delimiters
set AppleScript's text item delimiters to {" name="}
set nameElements to text items of elementInfo
if the (count of nameElements) > 1 then
set elementName to text item 2 of nameElements
set AppleScript's text item delimiters to {"\""}
set elementName to text item 2 of elementName
set the end of elementInfoText to "set theName to \"" & elementName & "\""
end if
set AppleScript's text item delimiters to {" id="}
set idElements to text items of elementInfo
if the (count of idElements) > 1 then
set elementID to text item 2 of idElements
set AppleScript's text item delimiters to {"\""}
set elementID to text item 2 of elementID
set the end of elementInfoText to "set theId to \"" & elementID & "\""
end if
set AppleScript's text item delimiters to {" class="}
set classElements to text items of elementInfo
if the (count of classElements) > 1 then
set elementClass to text item 2 of classElements
set AppleScript's text item delimiters to {"\""}
set elementClass to text item 2 of elementClass
set the end of elementInfoText to "set theClass to \"" & elementClass & "\""
end if
set AppleScript's text item delimiters to saveTID
return elementInfoText
end ParseElementInspector
Handler Script
use scripting additions
set the elementNumber to 0
--set theScript to JSForClickByID(theId)
--set theScript to JSForClickByName(theName, elementnum)
--set theScript to JSForClickByClassName(theClassName, elementnum)
--set theScript to JSForClickBytagName(thetagName, elementnum)
tell application "Safari"
activate
set safariJSScriptResults to do JavaScript " " & theScript in current tab of window 1
end tell
to JSForClickByID(theId)
return " document.getElementById('" & theId & "').click();"
end JSForClickByID
to JSForClickByName(theName, elementnum)
return " document.getElementsByName('" & theName & "')[" & elementnum & "].click();"
end JSForClickByName
to JSForClickByClassName(theClassName, elementnum)
return " document.getElementsByClassName('" & theClassName & "')[" & elementnum & "].click();"
end JSForClickByClassName
to JSForClickBytagName(thetagName, elementnum)
return " document.getElementsByTagName('" & thetagName & "')[" & elementnum & "].click();"
end JSForClickBytagName
NOTE: These two snippets are from a JavaScriptBrowser library I’m working on. When it’s ready I’ll share if anyone’s interested.