2

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

代码:inspect.exe 中树视图的屏幕截图,而后台应用程序图像

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 后端而无法识别元素

4

1 回答 1

2

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

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

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

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


编辑1:

为“win32”TreeView 尝试此代码,该代码不会自动检测为 TreeViewWrapper:

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请注意_treeview_element返回的对象get_item(...)表示没有窗口句柄的特定项目,但它是可用的。

于 2021-05-15T17:31:06.170 回答