How to wait for DOM elements before running script (administrator-layout-scripts)

Hello i am trying to automate rocketchat SSO screen by using this simple code:

document.getElementsByClassName(“rc-button rc-button–secondary external-login keyclock”)[0].click();

I am geting Exception in template helper: TypeError: Cannot read property ‘click’ of undefined

I am really novice when it comes to html,css,js frontend in general . From a google search i found out that my script is being executed before the DOM elements load which makes sense.

I found really hard to find a working solution to such a simple task .It’s a bit more tricky because this is runned on rocketchat administration scripts area.

The other solution i though is going directly into the code and adding the script after DOM elements but i have no idea where the code for the login page is located or if it’s even writtable , i installed using snaps.
Any ideas?