Ganesh S
Ganesh S

Reputation: 431

Pywinauto Windows Exists but not Visible

I am automating a third party application for a personal project using pywinauto. Facing a weird issue where a pywinauto returns True for a dialog.Exist but the dialog is not actually visible. As a result, since the code returns True, further action on the dialog fails. I believe application has cached the dialog or something else. Not sure how to handle this.

app = Application().connect(path = "D:/myapp/Trader.exe")   
existFlag = app.window_(title ="Trader - 23506").Exists(timeout =2) 
print existFlag  #Returns True
if(existFlag):
    app.window_(title ="Trader - 23506").Close()  #Fails

Here is the output

**True**
Traceback (most recent call last):
  File "myauto.py", line 792, in <module>
    app.window_(title ="Trader - 23506").Close()
  File "C:\Python27\lib\site-packages\pywinauto\application.py", line 357, in __getattribute__
    ctrls = self.__resolve_control(self.criteria)
  File "C:\Python27\lib\site-packages\pywinauto\application.py", line 239, in __resolve_control
    raise e.original_exception
pywinauto.findwindows.ElementNotFoundError: {'process': 12964, 'backend': u'win32', 'title': 'Trader - 23506'}

Upvotes: 3

Views: 8648

Answers (1)

Vasily Ryabov
Vasily Ryabov

Reputation: 9991

Yes, Win32 applications can have existing windows that are not visible. To check visibility you can call this:

visible = app.window(title ="Trader - 23506").is_visible()

Or it might be a timing issue (if fails in ~50% cases or existing window disappears right after .exists(...) call). So another way to handle closing the dialog is:

dlg = app.window_(title ="Trader - 23506")
try:
    dlg.wait_not('visible', timeout=2)
except Exception: # or timings.TimeoutError
    dlg.close()

Upvotes: 4

Related Questions