Selim
Selim

Reputation: 1

Javascript click() function clicking all the buttons at the same time

I want to click all the buttons at a website but my code tries to click all 2000 buttons at the same time and because of that chrome fails. How can I make it click one by one to the all buttons so it won't force the computer and chrome? I've thinked to count the index of the elements and have a for loop but I wonder is there any other ways or a basic code



const divs =
document.querySelectorAll('.wbloks_1');

divs.forEach(div => {

  div.click();

});

Chrome failed because of the overload

Upvotes: 0

Views: 107

Answers (2)

Giii
Giii

Reputation: 7

You can use setTimeout, this method introduces delays between clicks

const divs = document.querySelectorAll('.wbloks_1');
let index = 0;

function clickNext() {
  if (index < divs.length) {
    divs[index].click();
    index++;
    setTimeout(clickNext, 100); // Adjust the delay as needed
  }
}

clickNext();


Upvotes: -1

Elvis Adomnica
Elvis Adomnica

Reputation: 616

In case you haven't figured this out yet, and as I mentioned in my comment you could use setTimeout for this.

const divs = document.querySelectorAll('.wbloks_1');
const ms = 10; // whatever delay value you want

divs.forEach((div, index) => {
  setTimeout(() => { div.click(); }, index * ms);
});

Upvotes: 1

Related Questions