How to randomize click events using pyautogui in python

When automating tasks with python, it may be useful to randomize your click on a particular object. I frequently use the pyautogui library to identify an image to click on, then randomize the click location on the image.

If you use the built in click feature, it will only click the dead center of the object every time. This can be a give away that you are automating something and flag you as a bot. If we generate a random click coordinate you’ll have better luck.

This simple function will help avoid some simple bot detectors. It won’t fool Google’s latest recaptcha, but may improve results on previous versions or more simple bot detectors.


Let’s take a look at the function:

def randomClick(box):
    '''A function to return random click coordinates from box object by pyautogui'''
    x_click = int(random.uniform(box.left, box.left+box.width))
    y_click = int(random.uniform(box.top, box.top+box.height))
    return (x_click, y_click)

First, we define the function. You must pass it a “box” object that is generated by pyautogui. This is generated using the pyautogui.locateOnScreen() method:

import pyautogui as gui   # Imports the pyautogui library and calls it gui for easy reference
import random             # Needed for the function
objectLocation = gui.locateOnScreen('locate_this_image.png')

objectLocation will be a box object with the format (leftmost pixel, topmost pixel, width, height) of the found image.

We pass this object to our function combined with the pyautogui moveTo method, then tell pyautogui to click that coordinate:

gui.moveTo(randomClick(objectLocation))
gui.click()

The randomClick() function takes the box object and uses the random.uniform() function to get random x and y coordinates located in the box object. It returns the x and y for use in the moveTo() method. We then click in that location.


That’s it! You just used pyautogui and a custom python function to generate random x and y coordinates to click an object! Feel free to comment if you have any improvements or questions!


Full program text below:

import pyautogui as gui   # Imports the pyautogui library and calls it gui for easy reference
import random             # Needed for the function

def randomClick(box):
    '''A function to return random click coordinates from box object by pyautogui'''
    x_click = int(random.uniform(box.left, box.left+box.width))
    y_click = int(random.uniform(box.top, box.top+box.height))
    return (x_click, y_click)

objectLocation = gui.locateOnScreen('locate_this_image.png')
gui.moveTo(randomClick(objectLocation))
gui.click()

John

Leave a Reply

Your email address will not be published. Required fields are marked *