python – fastest way to click mouse

Question:

I’m trying to make an auto click bot in python using pyautogui but this takes too much time (it runs in a loop, and xPos/yPos changes every time). What faster ways are there? Thanks for helping.

pyautogui.PAUSE = 0.001
pyautogui.click(xPos,yPos,button = 'left')
Asked By: Itay Tsuk

||

Answers:

You can use pynput:

from pynput import mouse
from pynput.mouse import Controller, Button
import time

mouse = Controller()
one = time.time_ns()
for i in range(1000):
    mouse.click(Button.left)

two = time.time_ns()

print(two-one)

With this setup im able to execute 1000 clicks in .53 seconds.

Answered By: Lukas Neumann

I’m testing three library: Pyautogui, Mouse and Pynput.
Links to libs:

Below code and result:

PYAUTOGUI version:

import pyautogui
def click_pyautogui(x, y, button):
    pyautogui.moveTo(x, y)
    pyautogui.click(button=button)

MOUSE version:

import mouse
def click_mouse(x, y, button):
    mouse.move(x, y, absolute=True)
    mouse.click(button=button)

PYNPUT version:

from pynput.mouse import Button, Controller
def click_pynput(x, y, button):
    mouse = Controller()
    mouse.position = (x, y)
    button = Button.left if button=='left' else Button.right
    mouse.click(Button.left)

wrapper:

def click(x, y, button):
    # pyautogui
    # click_pyautogui(x, y, button)

    # mouse
    # click_mouse(x, y, button)

    # pynput
    click_pynput(x, y, button)

import timeit
if __name__ == '__main__':
    print(timeit.timeit("click(random.randrange(100), random.randrange(100), 'left')", number=100, globals=locals()))

RESULTS time for 100 cycles (average of 3, very small variations):

  • Pyautogui: 22.07 sec
  • Mouse : 0.16 sec
  • Pynput : 0.20 sec

Mouse look as fastest library!

Answered By: swasher

I can’t comment so I have to make a post, I just wanted to say that @swasher ‘s speed test is incorrect.

from pynput.mouse import Button, Controller
def click_pynput(x, y, button):
    mouse = Controller()
    mouse.position = (x, y)
    button = Button.left if button=='left' else Button.right
    mouse.click(Button.left)

This creates a new Controller object every time click_pynput is called, which is unnecessary and slow.

Creating the Controller object once before the function declaration is much better:

from pynput.mouse import Button, Controller
_mouse = Controller()
def click_pynput(x, y, button):
    _mouse.position = (x, y)
    button = Button.left if button=='left' else Button.right
    _mouse.click(Button.left)

This in fact showed (on my pc) that pynput and mouse have the same speed.

Real RESULTS time for 100 cycles (average of 3, very small variations):

  • Mouse : 0.1317509999498725 sec
  • Pynput : 0.1323150999378413 sec

Also I tested just move speed and pyinput is slightly faster (1000000 iterations):

  • Mouse : 24.206686099991202 sec
  • Pynput : 20.718958700075746 sec
Answered By: Dj Frixz
Categories: questions Tags: , , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.