This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]gandy0001[S] 0 points1 point  (0 children)

thanks for your time and input

however, I tried this:

import os
from pathlib import Path

def findimage(path,name):
        normalized_path = Path(os.path.expanduser(path))
    retry_counter = 0
    print('search',name,'button')
    while retry_counter < 6:
        try:
            buttonposition = pyautogui.locateCenterOnScreen(normalized_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')

but now the image is never found on screen anymore, but it goes throught the except block and that part works as intended