Issue
I'm trying to click on next link on a html page but having difficulties.
I have to get elementbyclassname of the main div and clicking on the second element of the div, here is the HTML of page i want to click:
<div class="next-previous">
<a href="http://website.net/560339/1/">Previous </a>
<a href="http://website.net/560339/2/">Next</a>
</div>
Here is the Javascript what I have tried so far.
var nextlink = document.getElementsByClassName('next-previous');
nextlink[0].click();
Solution
You're clicking on the div
, which has no effect. You need to click on the a
.
Replace nextlink[0].click()
with nextlink[0].lastElementChild.click()
and it should work.
Demo (replaced link targets with alerts):
var nextlink = document.getElementsByClassName('next-previous');
nextlink[0].lastElementChild.click();
<div class="next-previous">
<a href="javascript:alert('Previous')">Previous </a>
<a href="javascript:alert('Next')">Next</a>
</div>
Answered By - Siguza
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.