Mark Halls Mark Halls - 4 months ago 44
Python Question

PyAutoGui click permissions error

I have a very strange problem that I haven't seen happen in python before.

I have a script that works flawlessly on one pc and when I try to use it on another my defined function fails.

I'm using PyAutoGUI to automate some processes.

import csv
import pyautogui

pyautogui.PAUSE = 0.50
pyautogui.FAILSAFE = True


#click function requires arguments ('fullPathToImage', "Error Identifier")
def click(fullPathToImage, error):
try:
pyautogui.click(pyautogui.center(pyautogui.locateOnScreen(fullPathToImage)))
except:
print(error, " not found, trying again")
click(fullPathToImage, error)

def start():
click('C:/projects/images/test.png', "test.png")
pyautogui.typewrite("This is my test text")

if __name__ == '__main__':
start()


What is happening on this other machine is when it locates the image, it moves the mouse and clicks as expected in the try statement but then it immediately executes the except statement too.

The only difference between our two machines is I'm running pillow 3.1.1 and the one it doesn't work on is running pillow 3.3.0.

My instinct is something changed that isn't returning a success flag on click which is triggering the exception. I don't know why that would be the case since all pillow is used for is image recognition.

Admittedly I'm pretty new to error catching and I'm not sure where to continue from here. Any help would be greatly appreciated.

edit: the reason for calling the click function in the exception is to eliminate wait statements during loading screens. depending no the amount of data being processed it's difficult to preprogram delays.

Answer

So it turns out this was due to a permissions error on this machine. Due to it being a business computer the user didn't have admin rights. This caused clicks to be registered and then immediately triggered a WinError 5 exception. I solved this by adding another exception to my try block. "except PermissionError: pass" See below for implementation

import csv
import pyautogui

pyautogui.PAUSE = 0.50
pyautogui.FAILSAFE = True


#click function requires arguments ('fullPathToImage', "Error Identifier")
def click(fullPathToImage, error):
    try:
        pyautogui.click(pyautogui.center(pyautogui.locateOnScreen(fullPathToImage)))
##################################    
    except PermissionError:
        pass
##################################    
    except:
        print(error, " not found, trying again")
        click(fullPathToImage, error)

def start():
    click('C:/projects/images/test.png', "test.png")
    pyautogui.typewrite("This is my test text")

if __name__ == '__main__':
    start()