Search code examples
pythonpython-3.xpywinautopyautogui

How to wait for the specific image to appear on screen using pyautogui?


I have one application which I am able to automate using the pyautogui module. I am keeping the reference image in the script directory, and after some sleep time, I am able to complete it. In some builds, the specific image takes a while to appear. After which my script throws attribute not found error.

I need to know how to control pyautogui to wait until the image appears on-screen and, after that, proceed for the next execution.

I have searched many stack overflow questions where I found wait_until() in the pywinauto module where a script waits for specific window. In the same way, do we have anything for pyautogui to wait for a specific image to appear on-screen?

Python version used: 3.6.


Solution

  • This will wait until it finds the image:

    icon_to_click = "Recycle Bin"
    
    r = None
    while r is None:
        r = pyautogui.locateOnScreen('rb.png', grayscale = True)
    print icon_to_click + ' now loaded'