Search code examples
python-3.xautomationpywinauto

Python Pywinauto detect window based on existence of certain class_name


Background: I am trying to automate an installer that will be distributed to a bunch of different computers. Some of these already have a MS distributable file, some of them don't. The ones without this file have this inside the window control identifiers:

child_window(class_name="SysHeader32")

The reason this is important is that this will be an extra step in the installation that needs to have a button pressed. Is there a way to make an if loop similar to:

if main_dlg.child_window(class_name="SysHeader32") exists:
     click install
     proceed normally
else:
     Proceed normally

How would I implement this?

I have it working without the extra step, but if this extra step is present, the install fails.


Solution

  • There is method .exists(timeout=5) which returns True/False instead of raising exception like other methods do. Of course, try-except block is also possible, but .exists() looks better as a logic than an error handling.

    BTW, else branch is not needed. Just proceed normally after the condition code is executed or not.