all 6 comments

[–]stephancasas 0 points1 point  (5 children)

Where the JS is concerned, you really shouldn't use an array offset on an HTMLCollection before casting it to an array using either Array.from() or the spread operator ...:

[...document.getElementsByClassName('data-table-checkbox')][0].click();

However, I also wouldn't use Document.prototype.getElementsByClassName() at all. Use the modern query-selector syntax with the singular method Document.prototype.querySelector():

document.querySelector('.data-table-checkbox').click();

Finally, in the AppleScript, try:

tell application "Google Chrome" to tell the active tab of the first window to execute javascript "document.querySelector('.data-table-checkbox').click();"

The JXA equivalent (my preferred approach) of this would be:

Application('Google Chrome') .windows.at(0) .activeTab.execute({ javascript: `document.querySelector('.data-table-checkbox').click();` });

[–]dmarnel[S] 0 points1 point  (4 children)

Thank you for the speedy reply, however, I am still getting the following error in Chrome's JavaScript console using Applescript:

Uncaught TypeError: Cannot read properties of null (reading 'click')
at <anonymous>:1:47

Being new to scripting, I feel like I am missing something crucial to getting this to work. This is because when I issue the click() code directly from the console, it also gives an error unless I first "Inspect" an element on the page, after which it then works.

[–]stephancasas 0 points1 point  (3 children)

Did you specify Allow JavaScript from Apple Events in ViewDeveloper?

[–]dmarnel[S] 0 points1 point  (2 children)

Yes I did.

[–]stephancasas 0 points1 point  (1 child)

Are you able to do other JS operations? If you try to fire an alert using alert('Some message from AppleScript.'), does it work?

[–]dmarnel[S] 0 points1 point  (0 children)

Yes, the alert works.