5

I'm trying to simulate human-like mouse movements using pyautogui or autopy

Do any of you know or can provide a good way?

Let's say I want to move from (0, 0) to (56, 200). If I use the pyautogui.moveTo(), It just jumps there. If I use the autopy.mouse.smooth_move(), it does the job but the movement is very fake.

I want it to move to random nodes on the screen but end up on the destination.

izhang05
  • 744
  • 2
  • 11
  • 26
SunAwtCanvas
  • 1,261
  • 1
  • 13
  • 38
  • 3
    This sounds like a machine learning problem, you should record the mouse behavior during your normal use and then try to simulate it. I don't think there's any readily available package out there for it. – anishtain4 Apr 21 '20 at 01:56
  • 2
    Have you tried using `pyautogui.moveTo()` to move mouse in small increments towards the target and applying a small random offset to the position? – Ted Klein Bergman Apr 21 '20 at 01:56
  • @TedKleinBergman yah! i am actually experimenting with that right now but the thing is if i put the `pyautogui.moveTo()` in a while loop, it does this very very slowly haha. Its like moving 1 pixel per 0.05 seconds – SunAwtCanvas Apr 21 '20 at 13:59
  • @Denise You should provide that code into the question; that might help us figure out what's going wrong. – Ted Klein Bergman Apr 21 '20 at 16:49

1 Answers1

4

.moveTo() has two other parameters you can pass to help mimic human behavior, duration and tween. The first is how long it takes to get to the specified point, the second one changes its behavior such as: start fast and end slow, start slow and end fast etc...

Check out this section for reference.

I also imported random to create a random float to pass for the duration parameter, I think it helps make the mouse have more humanlike behavior.

from random import *
import pyautogui as py


py.moveTo(720, 360, uniform(0.6, 2.7), py.easeOutQuad)
py.moveTo(450, 900, uniform(0.6, 2.7), py.easeOutBack)
py.moveTo(360, 720, uniform(0.6, 2.7), py.easeInOutQuad)

If you are using an IDE, type pyautogui.ease (or py.ease) and you'll see that popup window which shows you all the variations you can try.

As a side note, human mouse behavior often has a curve to it and .moveTo() moves in a linear line. This won't be a problem unless whatever program you're interacting with doesn't want robotic behavior (such as online video games). They could detect that all your movement is linear and then ban your account so be aware (or maybe make your own function that adds a curve to the mouse movement).

Andrew Stone
  • 1,000
  • 1
  • 7
  • 17