2

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.

Vasily Ryabov
  • 9,386
  • 6
  • 25
  • 78
576i
  • 7,579
  • 12
  • 55
  • 92

2 Answers2

3

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())
576i
  • 7,579
  • 12
  • 55
  • 92
0

find_windows is very low-level entry point for automation. Using WindowSpecification objects you can wait for opening desired dialog/control or just check if it exists (all with customized timeouts).

See more detailed explanation in the Getting Started Guide.

You can use exists() or visible() methods (return boolean value) instead of wait('exists') or wait('visible') which can raise an exception if failed.

For your case it may look so:

static = app.DialogName.child_window(title_re='.*Please contact your system administrator.',
                                     class_name_re='Static')
if static.exists(timeout=20): # if it opens no later than 20 sec.
    app.DialogName.OK.click()
Vasily Ryabov
  • 9,386
  • 6
  • 25
  • 78
  • Thanks, I spent a lot of time reading the getting started guide. Your approach requires that I need to know **Dialogname**. Finding out that is currently a bit of a challenge. Will this approach work if there are multiple dialogs with the same title (of which only one has the desired text)? How will `app.DialogName.OK.click()` know that it needs to click on `static` and not on any other window of the same name? – 576i Nov 08 '16 at 16:43
  • For default `backend='win32'` you can use `app.windows()` to deal with all top level windows. Say `print [wnd.window_text() for wnd in app.windows()]`. That's how to know `DialogName`. – Vasily Ryabov Nov 09 '16 at 10:34
  • If there are multiple dialogs with the same name, it's possible to use `app.window(title='Dialog name', active_only=True)` instead of `app.DialogName` to disambiguate search criteria. Or even more detailed choice: `app.window(title='Dialog name', found_index=0)`. – Vasily Ryabov Nov 09 '16 at 10:35