Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Click all buttons on page with same class Javascript

I'm trying to click all buttons on a page with the class "btn btn-primary UnFollowUser".

Here is the script I have tried using

var buttons = document.getElementsByName('UnFollowUser');

for(var i = 0; i <= buttons.length; i++)  
   buttons[i].click();

But that throws the error:

VM336:5 Uncaught TypeError: Cannot read property 'click' of undefined(…)(anonymous function) @ VM336:5InjectedScript._evaluateOn @ VM158:878InjectedScript._evaluateAndWrap @ VM158:811InjectedScript.evaluate @ VM158:667

Any ideas?

like image 481
Savant Avatar asked Jul 21 '26 02:07

Savant


1 Answers

You are using getElementsByName instead of getElementsByClassName

 var buttons = document.getElementsByClassName('UnFollowUser');

 for(var i = 0; i < buttons.length; i++)  
     buttons[i].click();
like image 134
User K Avatar answered Jul 23 '26 16:07

User K