pywinauto / pywinauto

Windows GUI Automation with Python (based on text properties)
http://pywinauto.github.io/
BSD 3-Clause "New" or "Revised" License
5k stars 697 forks source link

The window has not been focused due to COMError: (-2147467259, 'Unspecified error', (None, None, None, 0, None)) #1103

Open bernd-wechner opened 3 years ago

bernd-wechner commented 3 years ago

Expected Behavior

No warning. And/or better feedback and understanding of what is going on.

Actual Behavior

I start my app with pywinauto, and get the top window, then I carry on automating fetch the menus and toolbars and starts on my merry way. It's working quite well. But my output is polluted often, with one instance and one instance only, and usually early around the time top_window is fetched not long after mostly and sometimes later, with a message that reads:

The window has not been focused due to COMError: (-2147467259, 'Unspecified error', (None, None, None, 0, None))

I can see that this is generated by set_focus() in uiawrapper.py but says nothing of utility.

FIxes I would recommends, might include:

  1. reporting the error with its Hex code not as a decimal number as that is how they are documented: https://docs.microsoft.com/en-us/windows/win32/com/com-error-codes-1
  2. Including more information about which element we were trying to focus when the error arose. Like it's name or even a complete element_info dump.
  3. Help us understand what role COM plays in this, a quick and simple pointer? Not least because I actually talk to the same app over its COM interface as well and wasn't aware that pywinauto used one. To what end is pywinauto talking over COM? Or this a windll internal thing? Either way I would love to understand it when it arises.

Steps to Reproduce the Problem

Not 100% sure it's easy to reproduce as it could be very contextual to this app, my platform whatever. it's a COM error after all. And an unspecified one. I do have a feeling, not a knowing, just a feeling that it arises then and when I interact with the PC while my automation script is running. If I stand back and let it do it's work, and don't touch my PC I don't see this warning (I think). If I continue to do something on the side, my automation actually works fine, nothing fails, every now and then it grabs my mouse pointer and whisks it away to click some menu on my app on the other monitor, but on the whole it all works fine. I don't do this often, but I am often doing a quick interaction moving a window after I started it ...

Specifications

bernd-wechner commented 3 years ago

For what it's worth I changed the output locally to:

warnings.warn('COMError: {} while setting focus on: {} {}'.format(hex(exc.hresult & 0xffffffff), self.element_info.control_type, self.element_info.name), RuntimeWarning)

which is much more useful to me. I can see the COM error code, I can see what element it was trying to focus.

Alas self.element_info does not have a very good __repr__ here as it leaves out the control_type.

manju1847 commented 5 days ago

@bernd-wechner Did you find any solution for this issue. I am seeing same issue while trying to access popup menu item.

Trying to access popup menu item throws below error RuntimeWarning: Can't get elements due to COM error: (-2147467259, 'Unspecified error', (None, None, None, 0, None)). Try to set pywinauto.windows.uia_element_info.UIAElementInfo.use_raw_view_walker = True

If I try setting UIAElementInfo.use_raw_view_walker = True, it throws below error RuntimeWarning: Can't get descendant elements due to error: (-2147467259, 'Unspecified error', (None, None, None, 0, None))

bernd-wechner commented 5 days ago

I admit this was some while ago and I can't rightly recall. That said I don't think I did ever work out what's going on here and worked around it by running the automation on another machine (but my desktop) so that my use of the desktop could not interfere with the automation run in any way.