pyautogui locateOnScreen无法识别启动时弹出的错误弹窗求助
Let's break down why your current script isn't catching the error dialog consistently, and walk through some actionable fixes to make it reliable:
Your current code runs pyautogui.locateOnScreen("ok.png") a single time after 20 seconds. If the error dialog pops up after that one check, your script will miss it entirely. Instead, you should repeatedly check for the dialog over a longer window of time, with short pauses between checks.
Here's how to adjust that part of your code:
import pyautogui, time 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") print("Waiting for Edgewise and checking for error dialog...") # Set a timeout (e.g., 60 seconds total to wait for the dialog) timeout_end = time.time() + 60 dialog_found = False while time.time() < timeout_end: # Look for the OK button screenshot a = pyautogui.locateOnScreen("ok.png", confidence=0.8) if a is not None: print("Found error dialog! Clicking OK...") # Adjust coordinates and click click_x = a[0] + 10 click_y = a[1] + 10 pyautogui.moveTo(click_x, click_y, duration=1) pyautogui.click() dialog_found = True break # Wait 2 seconds before checking again to reduce resource usage time.sleep(2) if not dialog_found: print("No error dialog appeared within the timeout window.") input("Finished")
confidence and grayscale By default, locateOnScreen requires a perfect pixel match, which fails easily if the dialog's appearance shifts (e.g., due to screen scaling, slight color differences, or window themes). Adding these parameters will make detection more robust:
confidence=0.8: Allows for 80% match accuracy (you'll need to installopencv-pythonfirst withpip install opencv-pythonfor this to work).grayscale=True: Ignores color differences and speeds up detection.
Update your locate line to:
a = pyautogui.locateOnScreen("ok.png", confidence=0.8, grayscale=True)
A hardcoded 20-second sleep is unreliable — Edgewise might load faster or slower depending on your system. Instead, wait until the Edgewise window is actually visible before starting to check for the error dialog:
# Wait for Edgewise window to appear (replace with your actual window title) edgewise_timeout = time.time() + 30 edgewise_running = False while time.time() < edgewise_timeout: # Check for any window with "Edgewise" in the title windows = pyautogui.getWindowsWithTitle("Edgewise") if windows: edgewise_running = True print("Edgewise is running, starting dialog check...") break time.sleep(1) if not edgewise_running: print("Edgewise failed to start within timeout.") input("Finished") exit() # Now start your dialog detection loop here...
ok.png screenshot Double-check that your screenshot is:
- A clean capture of only the OK button (no extra background or window borders).
- The exact same size and resolution as it appears on your screen (high-DPI displays can scale UI elements, which breaks pixel matching).
- Captured from the same display you're running the script on.
Image recognition can be flaky — for a more robust solution, use Windows API tools to directly find the error dialog and click its OK button. This avoids relying on screenshots entirely.
First, install pywin32 with pip install pywin32, then use this helper function:
import win32gui import win32api import win32con def click_error_dialog_ok(dialog_title_keyword="Error", button_text="OK"): # Callback to find visible windows matching the dialog title def window_callback(hwnd, extra): if win32gui.IsWindowVisible(hwnd): title = win32gui.GetWindowText(hwnd) if dialog_title_keyword in title: extra.append(hwnd) return False # Stop searching once found return True dialogs = [] win32gui.EnumWindows(window_callback, dialogs) if dialogs: dialog_hwnd = dialogs[0] # Find the OK button within the dialog button_hwnd = win32gui.FindWindowEx(dialog_hwnd, None, "Button", button_text) if button_hwnd: # Send a click message to the button win32api.SendMessage(button_hwnd, win32con.BM_CLICK, 0, 0) return True return False
You can then call this function in your loop instead of using image recognition.
内容的提问来源于stack exchange,提问作者Lewis Boxer

