How to properly search for and randomly click multiple images with pyautogui?

Question:

One of my initial python courses automates a simple cookie clicking game by using pyautogui.click at specific cordinates. I am trying to take this further by using the locateonscreen image functions and the random module to locate images and then randomly click within the images as I think this is more practical for my learning and more human-like.

When the images are found – everything works. When the images are not found – I get an AttributeError: 'NoneType' object has no attribute 'left' because my box does not exist in that case. Im looking for help programming the logic to try to find and imagine and if it finds it randomly click it, otherwise try to find the next image.

Here is what I have working when images exist:
The while coordinates are to click a static location, then after the counter reaches a certain point look for and randomly click the images. Then return to the static location to continue clicking and loop.

import pyautogui as gui   
import random             

gui.PAUSE = 0.01

gui.moveTo(x=383,y=576)
counter = 1
while gui.position() == (383,576):
    gui.click()
    counter += 1
    if counter % 300 == 0:
           
        def randomClick(box):
            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)

        Bank = gui.locateOnScreen('Bank.png')
        gui.moveTo(randomClick(Bank))
        gui.click() 
 
        def randomClick(box):
            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)

        Factory = gui.locateOnScreen('Factory.png')
        gui.moveTo(randomClick(Factory))
        gui.click() 
       
        def randomClick(box):
            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)

        Mine = gui.locateOnScreen('Mine.png')
        gui.moveTo(randomClick(Mine))
        gui.click()  
        
        def randomClick(box):
            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)

        Farm = gui.locateOnScreen('Farm.png')
        gui.moveTo(randomClick(Farm))
        gui.click()  
        
        def randomClick(box):
            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)

        Grandma = gui.locateOnScreen('Grandma.png')
        gui.moveTo(randomClick(Grandma))
        gui.click()   
    
        def randomClick(box):
            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)

        Cursor = gui.locateOnScreen('Cursor.png')
        gui.moveTo(randomClick(Cursor))
        gui.click()
        gui.moveTo(x=383,y=576)
Asked By: Divine Machines

||

Answers:

Depending on the version of pyautogui, when it doesn’t find the image it will either raise an exception or return None. Seems your version returns None, so all you need to do is after calling the locate function, just check if it didn’t return None.

Bank = gui.locateOnScreen('Bank.png')
if Bank is not None:
    gui.moveTo(randomClick(Bank))
    gui.click()

Also note that functions only need to be defined once, so you should define randomClick just once at the very beginning of you program. Then you can call it however many times you want.

Answered By: Filip Müller
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.