Discussions » Creation Requests

How to select a random generated ID?

§
Posted: 2022.01.06.

I want to click on a button automatically, but the button has a random generated ID each time like 081ab90900313289a80c11ce8af2b7ccfcbb0ecef313807f2fa8e0b229cb2fc1. I know there has to be an elegant way how to achieve that, but didn't find it myself yet. I want to use it like this:
(function (){document.querySelector('button[id="randomID"]').click();
Thank you!

§
Posted: 2022.01.06.
const idRegex = /[a-z0-9]{64}/;
const buttons = document.querySelectorAll('button');

for (const btn of buttons) {
  if (idRegex.test(btn.id)) {
    btn.click();
    break;
  }
}
§
Posted: 2022.01.06.
Edited: 2022.01.06.

This should click on first closest button from querySelectorAll('button') with id such like that

§
Posted: 2022.01.06.
const idRegex = /[a-z0-9]{64}/;
const buttons = document.querySelectorAll('button');

for (const btn of buttons) {
  if (idRegex.test(btn.id)) {
    btn.click();
    break;
  }
}

Awesome! It works! Thank you very much!
Is it possible to add some wait time before the click somewhere?

window.addEventListener('load', function() {

const idRegex = /[a-z0-9]{64}/;
const buttons = document.querySelectorAll('button');

for (const btn of buttons) {
  if (idRegex.test(btn.id)) {
    btn.click();
    break;
  }
}

(function (){
    document.querySelector('button').click();
  

  
})();
  }, false);
§
Posted: 2022.01.06.
window.addEventListener('load', function() {
  const someWaitTime = 1600;
  const idRegex = /[a-z0-9]{64}/;
  const buttons = document.querySelectorAll('button');

  for (const btn of buttons) {
    if (idRegex.test(btn.id)) {
      setTimeout(() => btn.click(), someWaitTime);
      break;
    }
  }
});
§
Posted: 2022.01.06.

Thank you very much!

Post reply

Sign in to post a reply.