In the following code, top_level_parent() throws an exception if the mouse cursor is over a "list item" of the Windows start menu (Like "Google Chrome" or "Notepad++")
x, y = win32api.GetCursorPos()
element_from_point = pywinauto.uia_defines.IUIA().iuia.ElementFromPoint(tagPOINT(x, y))
element_info = pywinauto.uia_element_info.UIAElementInfo(element_from_point)
wrapper = pywinauto.controls.uiawrapper.UIAWrapper(element_info)
wrapper_top_level_parent = wrapper.top_level_parent()
Traceback (most recent call last):
File "C:\Users\d_pra\PycharmProjects\pywinauto_recorder\pywinauto_recorder\recorder.py", line 270, in get_wrapper_path
wrapper_top_level_parent = wrapper.top_level_parent()
File "D:\Python\lib\site-packages\pywinauto\base_wrapper.py", line 439, in top_level_parent
return self.parent().top_level_parent()
File "D:\Python\lib\site-packages\pywinauto\base_wrapper.py", line 439, in top_level_parent
return self.parent().top_level_parent()
File "D:\Python\lib\site-packages\pywinauto\base_wrapper.py", line 433, in top_level_parent
parent = self.parent()
File "D:\Python\lib\site-packages\pywinauto\base_wrapper.py", line 409, in parent
return self.backend.generic_wrapper_class(parent_elem)
File "D:\Python\lib\site-packages\pywinauto\controls\uiawrapper.py", line 186, in __new__
return super(UIAWrapper, cls)._create_wrapper(cls, element_info, UIAWrapper)
File "D:\Python\lib\site-packages\pywinauto\base_wrapper.py", line 126, in _create_wrapper
new_class = cls_spec.find_wrapper(element_info)
File "D:\Python\lib\site-packages\pywinauto\controls\uiawrapper.py", line 159, in find_wrapper
if element.control_type in UiaMeta.control_type_to_cls:
File "D:\Python\lib\site-packages\pywinauto\uia_element_info.py", line 250, in control_type
return self._get_control_type()
File "D:\Python\lib\site-packages\pywinauto\uia_element_info.py", line 112, in _get_current_control_type
return IUIA().known_control_type_ids[self._element.CurrentControlType]
KeyError: 50039
The exception was raised because "semantic zoom" was not a control type known by my typelib wrapper module for UIAutomationCore.dll generated by comtypes package.
I fixed the issue deleting Lib\site-packages\comtypes\gen and regenerated it running python -m "pywinauto"
.
I think this problem is due to the OS I used when I launched Pywinauto the first time, it was Windows 7, it generated Lib\site-packages\comtypes\gen without SemanticZoom and AppBar. Now my OS is Windows 10.
According to https://learn.microsoft.com/en-us/windows/win32/winauto/uiauto-controltype-ids
UIA_AppBarControlTypeId Identifies the AppBar control type. Supported starting with Windows 8.1.
UIA_SemanticZoomControlTypeI Identifies the SemanticZoom control type. Supported starting with Windows 8.