Looking for a Javascript that do a left-mouse click on a image/button identified by ID or CLASS name, wait x seconds and repeat. And able to run in developer tools Console tap, in chrome and firefox.
Tried to write it myself, cause i figured it would be a simple code, but after 2 hours of trial and error with no luck, i am starting to run out of options.
Hopefully a Javascript pro out there got time to help out a very novice user ;)
Thanks
Method 1: Using the click() method: The click() method is used to simulate a mouse click on an element. It fires the click event of the element on which it is called. The event bubbles up to elements higher in the document tree and fires their click events also.
The HTMLElement. click() method simulates a mouse click on an element. When click() is used with supported elements (such as an <input> ), it fires the element's click event. This event then bubbles up to elements higher in the document tree (or event chain) and fires their click events.
Common events using this interface include click , dblclick , mouseup , mousedown . MouseEvent derives from UIEvent , which in turn derives from Event . Though the MouseEvent. initMouseEvent() method is kept for backward compatibility, creating of a MouseEvent object should be done using the MouseEvent() constructor.
What's wrong with
document.getElementById(id).click()
If you're willing to use jQuery, you can use this simple function to do it:
window.setInterval(function(){
$("#divToClick").trigger("click");
}, 1000);
That will call it every 1000 milliseconds, or 1 second
For a pure Javascript solution, you should take a look at How to trigger event in Javascript
or, if you're not interested in supporting IE, you can do it the simple way, with an Event()
constructor, and event.dispatch()
You'd use the event constructor and dispatchEvent
for that :
var support = true; // check if event constructor is supported
try {
if (new MouseEvent('click', {bubbles: false}).bubbles !== false) {
support = false;
} else if (new MouseEvent('click', {bubbles: true}).bubbles !== true) {
support = false;
}
} catch (e) {
support = false;
}
setInterval(function() {
if (support) {
var event = new MouseEvent('click');
}else{
var event = document.createEvent('Event');
event.initEvent('click', true, true);
}
elem.dispatchEvent(event);
},1000);
FIDDLE
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With