pywinauto Wait and focus

Tiago São José picture Tiago São José · Feb 4, 2015 · Viewed 8.5k times · Source

I'm trying to automate a windows application using pywinauto. I can select the menu and open the "open file window". I need to wait for this window to appear and then set focus to that window and click some buttons.

For some reason is not working.

def open_file():
    return pywinauto.findwindows.find_windows(best_match=u'Open File', class_name='#32770')[0]
pywinauto.timings.WaitUntilPasses(20, 0.5,open_file)
print('wait for window')
open_file.SetFocus()

When I try to run this it says that open_file doesn't have a SetFocus option.

I'm a beginner in pywinauto and I'm pretty sure that this is something easy to fix but I don't know how :/

Answer

Vasily Ryabov picture Vasily Ryabov · Feb 4, 2015

open_file is a function. It has no such method. It is much simpler to use Application object to wait dialog.

OpenDialog = pwa_app.window(best_match=u'Open', class_name='#32770').wait('visible', timeout=20, retry_interval=0.5)
OpenDialog.set_focus()

Low-level functions like wait_until_passes are already encapsulated inside wait and wait_not methods of class WindowSpecification.


Even more simple code should work:

pwa_app.OpenDialog.wait('visible', timeout=20)
pwa_app.OpenDialog.set_focus()