windows 使用 pywinauto 自动化的应用程序不检测 TreeView 中的元素,尽管元素有自己的特征

The windows application automating using pywinauto does not detect elements inside a TreeView, despite the elements have there own characteristic

我正在自动化的应用程序是一个支持 win32 的后端应用程序并使用 inspect.exe 来检测元素 下面是我尝试点击销售收据元素的代码,执行时出现错误

代码:screenshot of the treeview in inspect.exe while application image in background

app = Application(backend="win32").connect(process=5468)
app.windows()
dlg = app['TFMenuG.UnicodeClass']
handle = dlg.child_window(control_id='UIA_ButtonControlTypeId (0xC350)').draw_outline()

错误:

Traceback (most recent call last):
  File "c:\..\pythonDemo\notepad.py", line 62, in <module>
    handle = dlg.child_window(control_id='UIA_ButtonControlTypeId (0xC350)').draw_outline()
  File "C:\..\AppData\Local\Programs\Python\Python39-32\lib\site-packages\pywinauto\application.py", line 379, in __getattribute__
    ctrls = self.__resolve_control(self.criteria)
  File "C:\..\AppData\Local\Programs\Python\Python39-32\lib\site-packages\pywinauto\application.py", line 261, in __resolve_control
    raise e.original_exception
  File "C:\..\AppData\Local\Programs\Python\Python39-32\lib\site-packages\pywinauto\timings.py", line 436, in wait_until_passes
    func_val = func(*args, **kwargs)
  File "C:\..\AppData\Local\Programs\Python\Python39-32\lib\site-packages\pywinauto\application.py", line 222, in __get_ctrl
    ctrl = self.backend.generic_wrapper_class(findwindows.find_element(**ctrl_criteria))
  File "C:\..\AppData\Local\Programs\Python\Python39-32\lib\site-packages\pywinauto\findwindows.py", line 87, in find_element
    raise ElementNotFoundError(kwargs)
pywinauto.findwindows.ElementNotFoundError: {'control_id': 'UIA_ButtonControlTypeId (0xC350)', 'top_level_only': False, 'parent': <win32_element_info.HwndElementInfo - '', TFMenuG.UnicodeClass, 196780>, 'backend': 'win32'}

请帮我一种识别元素的方法。我怀疑由于 win32 后端

而无法识别这些元素

首先,如果您使用 Inspect.exe,则必须使用 Application(backend="uia")。如果您想检查应用程序与旧版“win32”后端的兼容性,您需要 Visual Studio.

中包含的 Spy++

第二个 control_id 是来自 Spy++ 的整数 ID,它可能与 运行 运行 不一致。我建议通过 print([w.window_text() for w in app.windows()]) 打印顶级 window 文本,并使用必要的文本来识别顶级 window 和转储子标识符:

app.window(title="Main Window Title").dump_tree() # or use title_re for regular expression
app.window(title="Main Window Title").child_window(title="Sales Receipts", control_type="TreeItem").draw_outline().click_input()
# or get .wrapper_object() and discover all available methods,
# wrapper methods can be chained as above

P.S。如果 Inspect.exe 不显示 属性“NativeWindowHandle”,则表示该元素对“win32”后端不可见。


编辑 1:

为未自动检测为 TreeViewWrapper 的“win32”TreeView 尝试此代码:

from pywinauto import Application
from pywinauto.controls.common_controls import TreeViewWrapper

app = Application(backend="win32").connect(class_name="TFMenuG.UnicodeClass")
dlg = app['TFMenuG.UnicodeClass']
handle = dlg.child_window(class_name='THTreeView.UnicodeClass').wrapper_object().handle
tree_view = TreeViewWrapper(handle)
print(dir(tree_view)) # list all available methods

tree_view.get_item("Sales Receipts").expand()
tree_view.get_item(r"Sales Receipts\Reports").click(where="text")

当您看到所有可用的方法时,请尝试“win32”TreeView 的记录方法:https://pywinauto.readthedocs.io/en/latest/code/pywinauto.controls.common_controls.html#pywinauto.controls.common_controls.TreeViewWrapper请注意 get_item(...) 返回的 _treeview_element 对象表示没有 [=35= 的特定项目] handle,但是可以用