skip to Main Content

I have TWO buttons on webpage with the following classes (below).

Only 1 of the 2 buttons work, not sure why the other is not working.

Button 1 (OK):

<button _ngcontent-vil-c68="" 
class="btn btn-success bet ng-star-inserted"><span _ngcontent-vil-c68="" 
 //Button 1 Click (Working)
 document.getElementsByClassName('btn btn-success bet ng-star-inserted')[0].click();

Button 2 (Not OK):

<div _ngcontent-vil-c66="" 
class="buttons"><button _ngcontent-vil-c66="" type="button" 
class="plus ng-star-inserted">

//Button 2 (Tried the following but not working)
document.getElementsByClassName('buttons')[0].click();
document.getElementsByClassName('plus ng-star-inserted')[0].click();
document.querySelector('.plus .ng-star-inserted').click();

Both buttons are from the same webpage.

2

Answers


  1. In the second case you try to click the div with the click() method but the div doesn’t have that method, which could cause an error that stops the code from running and the button is not clicked.

    Login or Signup to reply.
  2. The issue is this space here:

    document.querySelector('.plus .ng-star-inserted').click();
                                 ^
    

    A space in a selector means you look for any descendent matching the following condition, so in this case you are selecting any element with class ng-star-inserted which is a descendent of an element with class plus. But what you really want is one element with both classes, so the space must be removed:

    document.querySelector('.plus.ng-star-inserted').click();
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search