尽管元素具有自己的特征,但使用 pywinauto 自动化的 Windows 应用程序不会检测到 TreeView 中的元素 [英] The windows application automating using pywinauto does not detect elements inside a TreeView, despite the elements have there own characteristic
问题描述
我正在自动化的应用程序是一个支持 win32 的后端应用程序,并使用 inspect.exe 检测元素下面是我尝试点击销售收据元素的代码,在执行时出现错误
The application I am automating is a win32 supported backend application and using inspect.exe to detect the elements Below is my code trying to click on sales receipt element, on execution I get error
代码: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后端无法识别元素
Please help me a way to identify the elements. I am doubting the elements are not recognised because of win32 backend
推荐答案
首先,如果你使用 Inspect.exe
你必须使用 Application(backend=uia")
.如果您想检查应用程序与旧版win32"的兼容性后端,您需要包含在 Visual Studio 中的 Spy++.
First, if you use Inspect.exe
you must use Application(backend="uia")
. If you want to check the application compatibility with older "win32" backend, you need Spy++ which is included into Visual Studio.
第二个 control_id
是来自 Spy++ 的整数 ID,它在运行之间可能不一致.我建议通过 print([w.window_text() for w in app.windows()])
打印顶级窗口文本,并使用必要的文本来识别顶级窗口并转储子标识符:>
Second control_id
is integer ID from Spy++ and it can be inconsistent from run to run. I would recommend printing top level window texts by print([w.window_text() for w in app.windows()])
and use necessary text to identify top level window and dump child identifiers:
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
附言如果 Inspect.exe
未显示属性NativeWindowHandle",则表示该元素对win32"不可见;后端.
P.S. If Inspect.exe
doesn't show property "NativeWindowHandle", it means the element is not visible to "win32" backend.
试试这个win32"代码未被自动检测为 TreeViewWrapper 的 TreeView:
Try this code for the "win32" TreeView which is not automatically detected as 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"的文档化方法;树视图:https://pywinauto.readthedocs.io/en/latest/code/pywinauto.controls.common_controls.html#pywinauto.controls.common_controls.TreeViewWrapper 请注意get_item(...)
表示没有窗口句柄的特定项,但它是可用的.
When you see all available methods, try documented methods for "win32" TreeView: https://pywinauto.readthedocs.io/en/latest/code/pywinauto.controls.common_controls.html#pywinauto.controls.common_controls.TreeViewWrapper Please note that _treeview_element
object returned by get_item(...)
represents specific item without window handle, but it's usable.
这篇关于尽管元素具有自己的特征,但使用 pywinauto 自动化的 Windows 应用程序不会检测到 TreeView 中的元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!