Question on how to click a button using document.querySelector javascript

Hi experts!

I wish I knew how javascript and complicated websites work. But I don't, so I'm here asking for help.

In the screenshot I am including in this post, I'm trying to get KM to click the "update" button. From past experiences, I think I need to use the document.querySelector command, but I'm not sure what will go in there.

I'd appreciate any help I can get. Thanks!

-Erik

Can you give us a url for the actual page?

Unfortunately it's behind a password. I'm happy to copy/paste/screenshot anything that could be helpful. I'm attaching the source html if that's a good start.source.txt.zip (479.7 KB)

Thanks for your help!

-Erik

Yep, that's a good command. I use it a lot.

Given this HTML:

then this JavaScript should work, provided it is the first element with that attribute:

var btnElem = document.querySelector('button[data-automation="sdk-update-grade-button"]');
btnElem.click();

Put that in a Execute a JavaScript in Front Browser action.

No guarantees of course, since I can't test it. :wink:

Next time, if you can't provide the URL, then please provide a large block of the HTML code as text using a Forum Code Block, with "html" as the language.

Good luck.

1 Like

Thanks for the help JMichaelTX. I appreciate it!

When trying this code, I see the following error in the Chrome console: Uncaught TypeError: Cannot read property 'click' of null

Does this mean the click instruction isn't working in this situation? Or does it mean we're not properly identifying the button that needs clicking?

How do I tell if this is the first element with that attribute? Not sure if I should be looking at page source or frame source or something else to try to look.

Thanks for the pointer on the code block. I'll use that next time.

-Erik

While waiting for JMichaelTX to respond, you might upload your action with the JavaScript code. From the error message, it appears you haven't quite got it right. That's what the "null" indicates.

The key is "'click' of null".
the "null" means that the querySelector did NOT find the button element.

We really can't debug without the actual HTML code.