I am trying to create a script that clicks a button every 31 seconds.
That website contains many buttons with the classes reply-markup-button rp tgico
. To select the exact button I am looking for, I am looking for the exact text on the button using document.getElementByTagName
and a for-loop:
var viewAdsButton = document.getElementsByTagName("button");
var viewAdsButtonText = "View ads";
setInterval(function() {
for (var i = 0; i < viewAdsButton.length; i++) {
if (viewAdsButton[i].textContent == viewAdsButtonText) {
viewAdsButton[i].click()
}
}
}, 31000);
<button class='reply-markup-button rp tgico' onclick="console.log('button 1')">View ads</button>
<button class='reply-markup-button rp tgico' onclick="console.log('button 2')">View ads</button>
<button class='reply-markup-button rp tgico' onclick="console.log('button 3')">Don't View ads</button>
<button class='reply-markup-button rp tgico' onclick="console.log('button 4')">View ads</button>
The issue is that there are also many buttons with "View ads" on them, and I only want the script to click the first one instead of all of them.