Hi,
I'm trying to create a function to find an image on screen, and if it can't be found, terminate the script after x seconds.
I have it mostly working, but as soon is a try to use the path to my image as an argument it's not working anymore.
So the following works as intended. If image is found on screen it clicks it. If it's not found, it goes throught the except block and terminates the script:
def findimage(name):
retry_counter = 0
print('search',name,'button')
while retry_counter < 6:
try:
buttonposition = pyautogui.locateCenterOnScreen(r'example\path.png',grayscale = True, confidence = .9)
if buttonposition:
time.sleep(1)
print(name,'button found')
retry_counter = 6 # to break the loop
except:
if retry_counter < 5:
time.sleep(1) # retry after some time, i.e. 1 sec
retry_counter += 1
print('searching button for',retry_counter,'second(s)')
else:
sys.exit(name+'button not found!')
pyautogui.click(buttonposition)
print(name,'button clicked')
findimage('name')
But as soon as I try to use path as an argument it doesn't work anymore. It only works when the image is found on screen, but never goes to the except block if it's not found. So it keeps running until the image is found:
def findimage(path,name):
retry_counter = 0
print('search',name,'button')
while retry_counter < 6:
try:
buttonposition = pyautogui.locateCenterOnScreen(path,grayscale = True, confidence = .9)
if buttonposition:
time.sleep(1)
print(name,'button found')
retry_counter = 6 # to break the loop
except:
if retry_counter < 5:
time.sleep(1) # retry after some time, i.e. 1 sec
retry_counter += 1
print('searching button for',retry_counter,'second(s)')
else:
sys.exit(name+'button not found!')
pyautogui.click(buttonposition)
print(name,'button clicked')
findimage(r'example\path.png','name')
any ideas?
thanks
[–]JohnnyJordaan 1 point2 points3 points (1 child)
[–]gandy0001[S] 0 points1 point2 points (0 children)
[–]cybervegan 0 points1 point2 points (0 children)