Reputation: 113
I want to click a Like
button that have these properties:
<span class="_49vh _2pi7">Like</span>
I have tried the following code but it doesn't work in this case:
<script>
var inputs = document.getElementsByClassName('_49vh _2pi7');
for(var i = 0; i < inputs.length;) {
inputs[i].click();
}
</script>
Note: In my HTML page I just one Like
button.
Upvotes: 1
Views: 9433
Reputation: 54
Make sure that you that like button is not from Cross Origin (Facebook, Google or from different domain and not inside the Cors Iframe). You can't access cross origin elements in an iframe.
Upvotes: 0
Reputation: 390
You missed the i++
in the for loop. If you change the loop from for(var i=0; i<inputs.length;)
to for(var i=0; i<inputs.length; i++)
and it will work fine.
Another, much easier method is to use the jQuery library. Then you can simply write $("._49vh._2pi7").click()
(._49vh._2pi7
is the CSS selector for the button).
Upvotes: 0
Reputation: 759
I hope it's work`
<span class="_49vh_2pi7">Like</span>`
<script>
$('._49vh_2pi7').on('click',function(){
/////do somthing here.
});
</script>
Upvotes: 0
Reputation: 133403
As you have only one element directly use .click()
and also querySelector()
can be used to retrieve element
document.querySelector('._49vh._2pi7').click();
Upvotes: 0
Reputation: 22500
you are missing i++
increment in for loop
var inputs = document.getElementsByClassName('_49vh _2pi7');
for (var i = 0; i < inputs.length; i++) {
inputs[i].click=check
inputs[i].click();
}
function check(){
console.log('clicked')
}
<span class="_49vh _2pi7">Like</span>
Upvotes: 1