AppleScript click on button (chrome)

applescriptgoogle-chromejavascript

Here is the HTML tag of the button I would like to click using AppleScript.

<button class="btn btn-primary" autofocus="" ng-click="ok()" tabindex="0"><!-- ngIf: !isTranslate --><span ng-if="!isTranslate" class="ng-scope">OK</span><!-- end ngIf: !isTranslate --> <!-- ngIf: isTranslate --></button

Here is my script

tell application "Google Chrome"
    tell tab 2 of window 1 to set clickOnMyButton to execute javascript "document.getElementsByClassName('ng-scope')[0].click();"
end tell

Result:

missing value

how can I generate a click ?

Update : I found that this is causing the block

 style="z-index: 1050; display: block;"

Full HTML block

<div tabindex="-1" role="dialog" class="modal fade ng-isolate-scope in" ng-class="{in: animate}" ng-style="{'z-index': 1050 + index*10, display: 'block'}" ng-click="close($event)" modal-window="" size="sm" index="0" animate="animate" style="z-index: 1050; display: block;">
    <div class="modal-dialog modal-sm" ng-class="{'modal-sm': size == 'sm', 'modal-lg': size == 'lg'}"><div class="modal-content" modal-transclude=""><div class="modal-header ng-scope"><span class="information-icon glyphicon glyphicon-info-sign"></span> <!-- ngIf: !isTranslate --><span class="information-header ng-scope" ng-if="!isTranslate">Information</span><!-- end ngIf: !isTranslate --> <!-- ngIf: isTranslate --></div><div class="ng-scope"><span class="glyphicon modal-body glyphicon-menu-down" ng-class="{ 'glyphicon-menu-down' : !errorDetails, 'glyphicon-menu-right' : errorDetails}" ng-click="errorDetails = !errorDetails" tabindex="0"></span><!-- ngIf: !isTranslate --><span ng-if="!isTranslate" class="ng-scope">Details</span><!-- end ngIf: !isTranslate --> <!-- ngIf: isTranslate --></div><!-- ngIf: !errorBullets --><div ng-if="!errorBullets" collapse="errorDetails" class="ng-scope collapse in" style="height: auto;"><ul><li class="ng-binding">Please provide Purchase data</li></ul></div><!-- end ngIf: !errorBullets --><!-- ngIf: errorBullets --><div class="modal-footer ng-scope"><button class="btn btn-primary" autofocus="" ng-click="ok()" tabindex="0"><!-- ngIf: !isTranslate --><span ng-if="!isTranslate" class="ng-scope">OK</span><!-- end ngIf: !isTranslate --> <!-- ngIf: isTranslate --></button></div></div></div>
</div>

If I can't click on the menu, how can I remove this using AppleScript?

Best Answer

document.getElementsByClassName('ng-scope')[0].click(); is perfectly fine JavaScript, but it's targeting a <span> element (which usually don't have click() events associated with them), and not the <button> element (whose class attribute has value "btn-primary").

Therefore, your JavaScript needs to target btn-primary, like so:

document.getElementsByClassName('btn-primary')[0].click();

(where [0] will possibly need to be adjusted, depending on how many other elements before this one share its class name).

Alternatively, if you're confident that the correct <span> element is identified by the index 0, then you could access its parent element (which is the <button>) and issue the click() that way:

document.getElementsByClassName('ng-scope')[0].parentElement.click();

Without access to the webpage itself, though, I'm unable to test this rigorously, so, whilst I'm confident that the JavaScript principles are sound, you may need to do some tweaks of your own to isolate the element by using the correct class name and correct array index.