Simulate a mouse click in Javascript

Look for Javascript that does a left click on the image / button identified by the identifier or name CLASS, wait x seconds and repeat. And it can work in developer tools. Jib crane, in chrome and firefox.

I tried to write it myself, because I decided that it would be simple code, but after 2 hours of trial and error without luck, my options run out.

Hope Javascript has time to help a very novice user;)

thanks

+6
source share
3 answers

If you want to use jQuery, you can use this simple function to do this:

window.setInterval(function(){ $("#divToClick").trigger("click"); }, 1000); 

It will be called every 1000 milliseconds or 1 second.

For a clean Javascript solution, you should take a look at How to trigger an event in Javascript

or, if you are not interested in IE support, you can do it in a simple way, with the Event() and event.dispatch() constructor

+2
source

what happened with

 document.getElementById(id).click() 
+2
source

You used the event constructor and dispatchEvent to do this:

 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

+2
source

All Articles