7

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

4

3 Answers 3

2

What's wrong with

document.getElementById(id).click()
Sign up to request clarification or add additional context in comments.

2 Comments

That only works for input elements and links
2

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()

1 Comment

.trigger saves my life.
2

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

9 Comments

Tried the Javascript code you wrote on Fiddle, but does not click the image and returns no error. just output 44 and clicked.
Did of course replace test with actual ID.
@zyl1647 - That's what the console does when the same value is logged over and over, it just adds a number at the front for the number of times it was logged to the console, try this and see -> jsfiddle.net/hLnTy/4
Sadly the same, does however output [18, 18, 4, 3, 200, Array[3], "DBBBACCDECCD"] when i manually click the image while script runs
@zyl1647 - does the fiddle posted above actually output that, and in what browser
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.