How to let pyautogui recognize popup windows?

Question:

Apparently, pyautogui does not recognize the "real" screen when using the screenshots features.

The following simple code works fine in most cases, but not in all cases.
Apparently, some popup windows will not be recognized (e.g. web pages, Javascript(?), or some Windows messages).

import pyautogui

try:
    pyautogui.click('myButtonSomewhereOnTheScreen.png')
    print("Button clicked")
except:
    print("Error: Button not found")

This issue has been raised a few years ago but it was stated that it is a limitation in Pillow. Is there maybe any improvement over the years or does someone maybe know of a workaround or a different approach to include such popup windows?

Asked By: theozh

||

Answers:

Normally pyautogui cannot detect pop-ups, and as you mentioned this is a limitation of the Pillow library. In previous versions we could use pyautogui.locateonscreen but this would cause problems and now only works when the program has already started.

Answered By: ThisIsMatin

I solved this for my particular situation.

I could have the popups always consume the whole window. So when the popup happens, I know that the whole screen x through y is the popup.

So send pyautogui.click() and you will click the popup bringing it to the front (or however you say this)

now with the popup in focus, you can continue your way using pyautogui.locateOnScreen()

I hope that helps someone! This is the first time I have ever been able to provide a possible answer to something on stack overflow : )

Answered By: Fungi