如何获取Windows开始菜单中某个元素的top_level_parent()?

How to get top_level_parent() of an element in the Windows start menu?

在下面的代码中,如果鼠标光标位于 Windows 开始菜单的 "list item" 上方(如 "Google Chrome" 或 "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

引发异常是因为 "semantic zoom" 不是我的 typelib 包装器模块已知的用于由 comtypes 包生成的 UIAutomationCore.dll 的控件类型。 我修复了删除 Lib\site-packages\comtypes\gen 并重新生成 运行 python -m "pywinauto".

的问题

我认为这个问题是由于我第一次启动Pywinauto时使用的OS,它是Windows 7,它生成Lib\site-packages\comtypes\gen没有SemanticZoom和AppBar。现在我的 OS 是 Windows 10.

根据https://docs.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.