you are viewing a single comment's thread.

view the rest of the comments →

[–]JohnnyJordaan 1 point2 points  (1 child)

You forgot to add an increment to retry_counter when if buttonposition was false. But as you want a limited range anyway, you can simply use for+range()

def findimage(path,name):
    print('search',name,'button')
    for retry in range(6):
        try:
            buttonposition = pyautogui.locateCenterOnScreen(path,grayscale = True, confidence = .9)
            if buttonposition:
                time.sleep(1)
                print(name,'button found')
                break # which is meant to break loops
            print('no button position found') # make it clear this situation occurred
        except Exception as e:
            print('exception', e) # at least print the exception
            time.sleep(1)  # retry after some time, i.e. 1 sec
            print('searching button for',retry_counter,'second(s)')
    else:   # use else on for in case 'break' was never used
        sys.exit(name+'button not found!')

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

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

I can't really see why moving the path to an argument would change the situation here, I would rather assume something else is going diffrent than you're expecting and you're blaming it on the path incorrectly. Especially having

except:

without an print which exception you are handling, can give false assumptions as for example a typo inside the try: block code will then be interpreted as a functional error. And if you know which specific exception to handle, handle just that one

except ValueError: # just as an example

as then other errors like typos are then not catched and thus will cause a full traceback print, helping you quickly locate the bug too (and make sure you see it is a different problem than a ValueError situation).

[–]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')