Move mouse in screen and emulater a "Click" with PURE JS

58 views Asked by At

i trying make a code pure JS to move mouse in screen and simulate a click from user.

This not work for me: getElementBy....click(), forms[x].submit(), etc.

i need a function with pure js and that need: Move Mouse to x,y position in my screen. After that, using mouseEvents, "click" as a user clicking mouse.

I tried some simulate with key press, so KeyNumbers, "enter" with key13, not work to me also.

Anyone knows a way to do that, without any Lib ? (i found some libs doing that, but nothing with pure JS) i am a backend enginner, so its hard to me "deep dive" in pure JS mode.

If anyone can help me , I will be very grateful.

if anyone can made this function to me

2

There are 2 answers

0
vandre On

To simulate a click event take a look at MouseEvent

function simulateClick() {
  // Get the element to send a click event
  const cb = document.getElementById("checkbox");

  // Create a synthetic click MouseEvent
  let evt = new MouseEvent("click", {
    bubbles: true,
    cancelable: true,
    view: window,
  });

  // Send the event to the checkbox element
  cb.dispatchEvent(evt);
}
document.getElementById("button").addEventListener("click", simulateClick);

However there is no MouseEvent to move the mouse programmatically

0
GabrielB On

For security reasons you cannot simulate user actions in the browser. But using some software on OS side is possible