I want a script that clicks on this button every 3 seconds
<input type="submit" name="Submit" value="Puxar Alavanca">
It dsnt have any class, can you guys help me?
I want a script that clicks on this button every 3 seconds
<input type="submit" name="Submit" value="Puxar Alavanca">
It dsnt have any class, can you guys help me?
Share Improve this question asked Mar 5, 2017 at 16:42 user7662423user7662423 2-
1
Look into
setInterval
. – Carcigenicate Commented Mar 5, 2017 at 16:47 - Please see my updated answer, I have added a random time between 3 and 4 seconds as requested. – Chris Cruz Commented Mar 5, 2017 at 17:13
5 Answers
Reset to default 6This should do it
<input type="submit" name="Submit" id="but" value="Puxar Alavanca">
var but = document.querySelector("[name='Submit']");
setInterval(function () {but.click();},3000);
You can visit this link to know more https://www.w3schools./js/js_timing.asp
you can use jQuery to trigger a click event'
setInterval(function(){
$( "input" ).trigger("click");
},random(3000,4000));
function random(min,max){
return min + (max - min) * Math.random()
}
Try using setInterval() method
.document.querySelector()
selects your element,the setInterval() clicks
button on every 3 seconds and the click event is handled by a function
.
var btn = document.querySelector("[name='Submit']");
//console.log(btn);
setInterval(function(){
btn.click();
},3000);
//Handling of click event
btn.onclick=function(){
console.log('clicked');
}
<input type="submit" name="Submit" value="Puxar Alavanca">
The setInterval() method calls a function or evaluates an expression at specified intervals (in milliseconds).
Since you don't have a class name and such, you are going to want to make sure you only click the button you want when doing this.
With my version, it allows for gathering all the tags of type 'input' and clicking the one you requested only - every 3-4 (this number is randomly generated, sometimes 3 sometimes 4) seconds.
Please see below:
// Random time between 3 and 4 seconds.
var randomTime = (Math.floor(Math.random() * (4 - 3 + 1)) + 3) * 1000;
setInterval(function() {
var tags = document.getElementsByTagName("input");
for (var i = 0; i < tags.length; i++) {
if (tags[i].value == "Puxar Alavanca") {
tags[i].click();
// Below alert should be removed, just for testing/showing purposes.
alert("Button clicked after " + randomTime + "ms.");
}
}
}, randomTime);
<input type="submit" name="Submit" value="Puxar Alavanca">
var interval = 1000; // Click interval in milliseconds
function clickButton() {
var button = document.getElementById('buttonId'); // Replace 'buttonId' with the ID of the button you want to click
button.click();
}
setInterval(clickButton, interval);