I am trying to modify the script below to click a button that looks like this on a site:
<button id="checkPrice-02070" onclick="checkPrice(02070,null); return false;" class="orangeDark">
<span>check price</span>
</button>
I am using the code below. So far, the page seems to keep reloading; nothing else happens.
Any advice to someone new?
(function () {
window.addEventListener("load", function (e) {
clickConfirmButton()
}, false);
})();
function clickConfirmButton() {
var buttons = document.getElementsByTagName('button');
var clicked = false;
for (var index = 0; (index < buttons.length); index++) {
if (buttons[index].value == "check price") {
buttons[index].click();
clicked = true;
break;
}
}
if (!clicked) {
setTimeout("window.location.reload()", 300 * 1000);
}
}
A
<button>svalueis not the visible text. You’d want to searchtextContent.However:
If that sample HTML is correct, you’d be better off searching for ids that start with
checkPrice. See the code below.Are you sure you want to reload if the button is not found? If it is added by AJAX this is not the best approach. See this answer.
Don’t use
setTimeoutwith a string (to evaluate) argument like that. See the code below.You do not need to wrap the code in an anonymous function.
Anyway, this should work, given the sample HTML:
To check the button text anyway, use: