Search code examples
pythonpython-3.xautomationpywinauto

Pywinauto - How to read the text within a pop up window to identify it?


I'm trying to remote control a windows application that sometimes displays a warning window on startup using pywinauto.

The code below identifies the window because it has no menu.

I would like to read the pop up text to look for the phrase "Please contact your system administrator." within that pop up window to know that it's the right one.

mywindows = pywinauto.findwindows.find_windows(title_re=".*MyProgramTitle")

# proof that two windows are found
print(len(mywindows))

for handle in mywindows:
    print('\nhandle {}'.format(handle))

    app = Application().connect(handle=handle)
    navwin = app.window(handle=handle )

    if not navwin.menu_items():
        # no menu - I bet it's a pop up
        print('{} is the window I\'m looking for'.format(handle))
        navwin.print_control_identifiers()

The above code prints out all windows info, including "Static - 'Location mapping failed. Please contact your system administrator.'"

But I'd need to catch that printout to further process it.


Solution

  • As a hacky solution I went through the source code of print_control_identifiers() and found this way to loop through the controls of the window

    navwin.print_control_identifiers()
    
    for x in navwin.descendants():
        print (x.window_text())
        print (x.class_name())