Search code examples
pythonloopswhile-looppyautogui

I can't stop the loop in python


Here is my problem, I'm a beginner and I try to loop a "find and click" on a specific image ( the "NON" button ), it works but the loop don't stop when all the images are clicked.

How can I tell it that when there's no image left to stop the loop and continue with the code?

here's my code:

NON2 = pyautogui.locateCenterOnScreen('NON2.png', grayscale=True, confidence=0.9)

while NON2:
    time.sleep(2)
    click = pyautogui.locateCenterOnScreen('NON2.png', grayscale=True, confidence=0.9)
    pyautogui.click(click)

pyautogui.click(44,965)

and here's the screenshot of the "non" button:

NON button


Solution

  • DISCALIMER I don't know pyautogui.

    You'll never update the NON2 button in the loop. Try this:

    NON2 = pyautogui.locateCenterOnScreen('NON2.png', grayscale=True, confidence=0.9)
    
    while NON2:
        time.sleep(2)
        NON2 = pyautogui.locateCenterOnScreen('NON2.png', grayscale=True, confidence=0.9)
        pyautogui.click(NON2)
    
    pyautogui.click(44,965)