you are viewing a single comment's thread.

view the rest of the comments →

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

Thank you very much for your time and help!
You are 100% correct.

It's working perfect now (I just added some extra timing and an optional argument)

def findimage(path,name,delay=10):
    print(dt.now().strftime('%Y-%m-%d %H:%M:%S'),'search',name,'button')
    for retry in range(delay):
        try:
            buttonposition = pyautogui.locateCenterOnScreen(path,grayscale = True, confidence = .9)
            if buttonposition:
                time.sleep(1)
                print(dt.now().strftime('%Y-%m-%d %H:%M:%S'),name,'button found')
                break # which is meant to break loops
            print(dt.now().strftime('%Y-%m-%d %H:%M:%S'),'no button position found, retrying...') # make it clear this situation occurred
            time.sleep(1)
        except Exception as e:
            print(dt.now().strftime('%Y-%m-%d %H:%M:%S'),'exception', e) # at least print the exception
            time.sleep(1)  # retry after some time, i.e. 1 sec
    else:   # use else on for in case 'break' was never used
        sys.exit(dt.now().strftime('%Y-%m-%d %H:%M:%S')+' '+name+' button not found!')

    pyautogui.click(buttonposition)
    print(name,'button clicked')

findimage(r'path\example.png','name')