简体   繁体   中英

Unable to click a pagination next button for multiple times - javascript

I have written a script which clicks a specific button after a short interval of time. The button is click through javascript, and I put the interval of 2 seconds in between. But the script only executes once and only one time the button is clicked I don't know why :

Below is my script:

 var el = document.getElementsByClassName("paginate_button next")[0]; setInterval(function() { el.click(); console.log("Clicked !"); }, 2000); 

The pagination is given on this page . You can test the code on the console of that page.

The element is probably being removed and recreated when you advance to the next page. So do getElementsByClassName inside the setInterval callback, so that it retrieves the new element each time.

try to catch the element with jquery

 var el = $('.paginate_button.next') setInterval(function() { el.click(); console.log("Clicked !"); }, 2000); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <a class="paginate_button next" aria-controls="jf-table" data-dt-idx="7" tabindex="0" id="jf-table_next">Next</a> 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM