question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

pywinauto 0.6.5 invalid window handle problem

See original GitHub issue

My 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:closed
  • Created 5 years ago
  • Comments:13 (7 by maintainers)

github_iconTop GitHub Comments

1reaction
meshuggahtascommented, Aug 10, 2018

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).

0reactions
vasily-v-ryabovcommented, Nov 1, 2018

Filed #593 for this feature request.

Read more comments on GitHub >

github_iconTop Results From Across the Web

PDF - pywinauto Documentation
handle The windows handle of a window of the application, e.g. app = Application().connect(handle=0x010f0c) path The path of the executable ...
Read more >
Pywinauto: unable to bring window to foreground
I think the SetFocus is a bit buggy. At least on my machine I get an error: error: (87, 'AttachThreadInput', 'The parameter is...
Read more >
pywinauto 0.6.5 documentation
pywinauto是一组用于自动化Microsoft Windows GUI的python模块。 最简单的是,它允许您将鼠标 ... Added a check to ensure that a windows Handle is a valid window.
Read more >
pywinauto connect to existing window - You.com - You.com
The problem in hwnd , it is a list, but handle=hwnd requires an int. You should use find_window instead of find_windows or use...
Read more >
pywinauto Changelog - pyup.io
Fix UIA crash: handle InvalidControlType properly. * Fix menu_select() for one level main menu in ... 0.6.5 Handling Privileges, AutomationID for Win32 etc....
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found