Search code examples
python-3.xpyautogui

pyautogui locateonscreen not finding pop up


Im trying to automate a program, we use all the time:

import pyautogui, time
import PIL

input("Press Enter to start")
print("Starting program in 5 seconds")
time.sleep(5)

#open edgewise
pyautogui.hotkey("win")
pyautogui.typewrite("edgewise")
pyautogui.hotkey("Enter")

time.sleep(20)

print("Checking for error message")
a = pyautogui.locateOnScreen("ok.png")
while True:
    if a == None:
        print("Not Found")
        break
    else:
        print("Found")
        new0 = a[0]
        new0 = new0 + 10
        new1 = a[1]
        new1 = new1 + 10
        pyautogui.moveTo(new0,new1,duration=1)
        pyautogui.click()
        break

input("Finished")

So the idea of the program is that it opens the software, which during the startup it flashes up an error message (sometimes) which requires you to click ok.

Now, this program works if the program (edgewise) has already started and the error message is on-screen before I start the python script. I have even given it a sleep timer to make sure the program has opened correctly and is displaying the message in time

Any ideas


Solution

  • It seems that PYAutoGUI locateonscreen function only works when the program is already started, I tried to build my script to do everything including starting the application, but this causes issues.

    Hope this helps others