Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

pywinauto.findwindows.WindowNotFoundError in pywinauto

I am new in pywinauto and have just started learning. I have installed pywinauto with pip in my local 32 bit Windows 7 OS. So, I have this sample code to open an URL in chrome browser.

from pywinauto import application
app=application.Application()
app.start(r'C:\Program Files\Google\Chrome\Application\chrome.exe')
app.window_(title='New Tab')
app.window_().TypeKeys('{F6}')
app.window_().TypeKeys('{ESC}')
app.window_().TypeKeys('www.facebook.com')

On running it, it is throwing error:

Traceback (most recent call last):
File "pywinauto_nil.py", line 6, in <module>
app.window_().TypeKeys('{F6}')
File "C:\Python27\lib\site-packages\pywinauto\application.py", line 252, in    
__getattr__
    ctrls = _resolve_control(self.criteria)
File "C:\Python27\lib\site-packages\pywinauto\application.py", line 758, in _resolve_control
    raise e.original_exception
pywinauto.findwindows.WindowNotFoundError

I have googled, but could not find any helpful solution.

Where am I going wrong?

like image 537
niladri chakrabarty Avatar asked Aug 10 '16 06:08

niladri chakrabarty


People also ask

What is Pywinauto used for?

pywinauto is a set of python modules to automate the Microsoft Windows GUI. At its simplest it allows you to send mouse and keyboard actions to windows dialogs and controls, but it has support for more complex actions like getting text data.

How do you close windows in Pywinauto?

You can check to see if there's a specific hotkey to close the program. However, the easiest way to do this is to send Alt-F4.


2 Answers

The full answer would be long and complicated. Let's start from your small problem.

  1. Chrome spawns few more processes that is not connected with app object. Solution: use Application(backend="uia").connect(title='New tab') (or title_re or whatever possible for find_windows).

  2. The bigger problem is that Chrome controls cannot be detected and handled by default backend="win32" (or pywinauto 0.5.4 and before). MS UI Automation support has been introduced since 0.6.0. By the way, pywinauto/UIA can handle top-level windows in a process-agnostic way: Desktop(backend='uia').NewTab.type_keys('some_URL')

  3. One more detail about Chrome. It doesn't enable UIA support by default. To enable UIA accessibility it must run so: chrome.exe --force-renderer-accessibility. Though UIA mode is enabled by default in Firefox and Opera.

And finally pywinauto is not specifically designed for Web automation. Right now it might be combined with Selenium.

like image 117
Vasily Ryabov Avatar answered Oct 19 '22 01:10

Vasily Ryabov


To add to what Vasily said:

There might be another problem here.

If your Chrome is too slow to start, the connection might miss it.

I think this is somehow regulated in start method, but as chrome starts more than one process it may cause the problem.

I advise you to use Python's module "webbrowser" to start Chrome, then try Connecting to it with pywinauto.

There is an option, if I remember correctly, to wait until the window appears. Just specify the timeout. Otherwise try connect in a definite loop with some sleeping inbetween tries.

It may work, or it may not, depends whether UIA support is needed. If it is, you have to start Chrome with UIA support.

like image 38
Dalen Avatar answered Oct 19 '22 01:10

Dalen