pywinauto 0.6.5 invalid window handle problem
See original GitHub issueMy scripts got broken after updating from 0.6.4 to 0.6.5. Error thrown when using type_keys on a not focused element while automating an application with focus problems.
pywintypes.error: (1400, ‘GetWindowRect’, ‘Invalid window handle.’).
The program I automate made with Delphi. The program has a problem of getting focus after not having focus. So clicking a button in this application requires clicking twice if it didn’t had focus initially.
Detailed focus problem: When I try to get focus of the Delphi application, there will be no foreground application having focus for a very brief moment until getting focused. Used this program: http://www.happydroid.com/focus
1284:C:\Windows\explorer.exe | Thu Aug 09 12:24:18 2018
No foreground application | Thu Aug 09 12:24:19 2018
9840:C:\Program Files (x86)\Intellio Video System\Intellio Client 4\RCClient.exe | Thu Aug 09 12:24:20 2018
So I use win32 backend, type_keys (AFAIK SendKeys requires focus) and click (AFAIK click_input requires focus).
The release notes only talks about
Fix .set_focus() for “uia” backend including minimized window case (issue #443).
while I think I’m having this problem because one of the changes in hwndwrapper (https://github.com/pywinauto/pywinauto/commit/960682d0abd7db4efb8e129e4599fd47a4b5e5bd#diff-03fd5a827c53b158f63e6e8c518d9809)
I’m looking for your answer. And thank you for making pywinauto 😃
Relevant example code that fails everytime in 0.6.5 and works flawlessly in 0.6.4:
import pywinauto
APP = pywinauto.Application(backend="win32").start("RCClient.exe")
elem = APP.MainForm
elem.type_keys("%{TAB}") # everything fine here
elem.type_keys("%{TAB}") # error
Full log:
12:02:20.461 ERROR scenario Test @124 Traceback (most recent call last):
File "C:\Users\oliver.horvath\PycharmProjects\autoivs\scenario.py", line 113, in Test
RESULT = func() # Teszt eset futtatása
File "C:\Users\oliver.horvath\PycharmProjects\autoivs\scenarios\teszt.py", line 276, in Github2_9988
log.info(ivs.APP.MainForm.control_type())
File "C:\Users\oliver.horvath\venv\autoivspackages\lib\site-packages\pywinauto\application.py", line 368, in __getattribute__
ctrls = self.__resolve_control(self.criteria)
File "C:\Users\oliver.horvath\venv\autoivspackages\lib\site-packages\pywinauto\application.py", line 246, in __resolve_control
criteria)
File "C:\Users\oliver.horvath\venv\autoivspackages\lib\site-packages\pywinauto\timings.py", line 431, in wait_until_passes
func_val = func(*args, **kwargs)
File "C:\Users\oliver.horvath\venv\autoivspackages\lib\site-packages\pywinauto\application.py", line 191, in __get_ctrl
dialog = self.backend.generic_wrapper_class(findwindows.find_element(**criteria[0]))
File "C:\Users\oliver.horvath\venv\autoivspackages\lib\site-packages\pywinauto\findwindows.py", line 84, in find_element
elements = find_elements(**kwargs)
File "C:\Users\oliver.horvath\venv\autoivspackages\lib\site-packages\pywinauto\findwindows.py", line 303, in find_elements
elements = findbestmatch.find_best_control_matches(best_match, wrapped_elems)
File "C:\Users\oliver.horvath\venv\autoivspackages\lib\site-packages\pywinauto\findbestmatch.py", line 494, in find_best_control_matches
name_control_map = build_unique_dict(controls)
File "C:\Users\oliver.horvath\venv\autoivspackages\lib\site-packages\pywinauto\findbestmatch.py", line 473, in build_unique_dict
ctrl_names = get_control_names(ctrl, controls, text_ctrls)
File "C:\Users\oliver.horvath\venv\autoivspackages\lib\site-packages\pywinauto\findbestmatch.py", line 320, in get_control_names
non_text_names = get_non_text_control_name(control, allcontrols, textcontrols)
File "C:\Users\oliver.horvath\venv\autoivspackages\lib\site-packages\pywinauto\findbestmatch.py", line 219, in get_non_text_control_name
ctrl_r = ctrl.rectangle()
File "C:\Users\oliver.horvath\venv\autoivspackages\lib\site-packages\pywinauto\base_wrapper.py", line 337, in rectangle
return self.element_info.rectangle
File "C:\Users\oliver.horvath\venv\autoivspackages\lib\site-packages\pywinauto\win32_element_info.py", line 181, in rectangle
return handleprops.rectangle(self.handle)
File "C:\Users\oliver.horvath\venv\autoivspackages\lib\site-packages\pywinauto\handleprops.py", line 207, in rectangle
return win32structures.RECT(*win32gui.GetWindowRect(handle))
pywintypes.error: (1400, 'GetWindowRect', 'Érvénytelen ablakleíró.')
Issue Analytics
- State:
- Created 5 years ago
- Comments:13 (7 by maintainers)

Top Related StackOverflow Question
Thanks,
.type_keys("something", set_foreground=False)does solves my problem!And I can verify that my problem is directly connected with focus using enough
time.sleep()to have focus on the automated app.I don’t know if I have other problems related to this (focus problem not originating from pywinauto) but will report it back if I found any. (So you can close this issue if you want).
Filed #593 for this feature request.