5

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?

niladri chakrabarty
  • 503
  • 1
  • 7
  • 15

2 Answers2

6

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.

Vasily Ryabov
  • 9,386
  • 6
  • 25
  • 78
4

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.

Dalen
  • 4,128
  • 1
  • 17
  • 35