简体   繁体   中英

javascript simulate mouse click on specific position

I need to now how to fire a mouse click event on a button automatically. I have this but doesn't work :(

window.setInterval(function() { simulateClick(); }, 2000);

function simulateClick() {
    var evt = document.createEvent("MouseEvents");
    evt.initMouseEvent("click", false, false, window, 0, 684, 571, 684, 508, false, false, false, false, 0, null);
        var a;
    a.dispatchEvent(evt);
}

Thanks in advance

Oscar

If all you want to do is click a button, button elements have a click method that can be invoked:

<input type="button" id="theButton" onclick="javascript:alert('The button was clicked!');" value="A button" />

<script language="javascript">

setTimeout(function(){
    document.getElementById("theButton").click();
}, 1000); // wait one second then click the button

</script>

There's no need to "actually" simulate the mouse click at a specific x,y position.

我不知道它是否可以在javascript上执行此操作,但如果您需要自动和定期点击,也许您可​​以使用外部工具,如autohotkey

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM