A "for" loop for do a task

Hello,
I want to write a JS code in web console of the browser for click on a button. I did:

myclick = document.querySelector('button.sqdOP.L3NKy.y3zKF');
for (var input = 1; input <= 10; input++) {
 myclick.click();
}

But it just click on the button once. Which part of code is wrong?

Thank you.

Nothing seems wrong with the code.

Perhaps if you let us know the site and the button, we can help diagnose things further for you.

The “Follow” button of Instagram website.

No matter how many times you click that button, they know it’s coming from the same location so they record only one follow. It doesn’t matter how many times it’s clicked.

Thank you.
Any solution? Instagram.com just allow a limited follow per minute.

Nope. The rules are there for good reason. Don’t break the rules.

3 Likes

Granted, of course, that the button doesnt change any of its classes at any point (like say, when someone clicks the button), as a means of… i don’t know… specifically avoiding this sort of coding…

1 Like

If they do, then maybe you can write an interval to run the click action every 60 seconds.

I found below code:

var xpath = "//button[contains(text(),'Follow')]";
var listOfButtons = $x(xpath);

for(var i=0; i<listOfButtons.length; i++) {
  (function (i) {
    setTimeout(function () {
      listOfButtons[i].click() }, 1000*i);
  })(i);
};

But it show me an error:


Why?

As it’s unethical to spread code with the purpose to enable abuse of what the platform tries to block, this thread has been closed.

2 Likes