I have a problem somewhat related to a post that came up a few years ago. There was significant input to that post, as well as significant disagreement, and apparently no clear answer. So I thought I'd bring this up again but in the context of my specific issue.
The Goal:
Use AppleScript/Safari to click several elements of an external web-site. This is working fine except for the following 3 "buttons", all created within <div>
elements:
The "Call" button:
<div role="button" class="goog-inline-block jfk-button jfk-button-primary jfk-button-narrow" tabindex="0" style="-webkit-user-select: none;">Call</div>
The "Text" button:
<div role="button" class="goog-inline-block jfk-button jfk-button-primary jfk-button-narrow" tabindex="0" style="-webkit-user-select: none;">Text</div>
Note: these 2 elements are identical except for the tags "Call" vs "Text" at the end".
The "Send" button:
<div class="goog-button-base-content">Send</div>
What doesn't work:
tell application "Safari"
do JavaScript "document.getElementsByClassName('ClassNameHere')[n].click();" in document 1
-- n is the element number, i.e. 0 or 1)
end tell
This returns missing value
suggesting that the statement didn't return anything.
The problem:
- Some say
click()
will work in this setting, though it did not work as implemented above - Others say JQuery is needed. However, I need to use Applescript/Safari and I've read that JQuery is not natively available in this setting.
And finally, the Question:
When answering its best to assume I know nothing about Javacript/JQuery (I'm OK with AppleScript). So the best answer would provide a sample code that I can cut/paste into Applescript's do JavaScript
command.
If the above elements can be clicked via pure JavaScript (preferred), please provide a sample code.
If pure Javascript is not possible, and JQuery is needed, please provide an example that illustrates how JQuery can be implemented within AppleScript (is that was done in this answer?)
Any comments that help me understand will also be appreciated. Thanks in advance!
UPDATE: This returns missing value
as well.
tell application "Safari"
do JavaScript "var nlDivs = document.getElementsByTagName('DIV'); for (var i = 0; i < nlDivs.length; i++) {if (nlDivs[i].innerHTML === 'Send') nlDivs[i].click();" in document 1
end tell