前言
在使用pywinauto查找多个相同控件时,可能会遇到操作错误的问题。通过使用found_index参数,可以选择其中的一个控件来解决这个问题。
查找多个相同控件
我们将查找所有类型为control_type=”MenuBar”的控件。
代码示例
from pywinauto import Application <p>app = Application('uia').start("notepad.exe") win = app.window(title_re="无标题 - 记事本")</p><h1>输入内容</h1><p>win.child_window(title="文本编辑器").set_text("hello world")</p><h1>查找MenuBar</h1><p>menu = win.child_window(control_type="MenuBar") print(menu.window_text())
在执行menu.window_text()获取窗口文本时,会报错,因为找到了不止一个符合条件的控件。
错误信息
pywinauto.findwindows.ElementAmbiguousError: There are 2 elements that match the criteria {'control_type': 'MenuBar', 'top_level_only': False, 'parent': <uia_element_info.uiaelementinfo notepad="">, 'backend': 'uia'}
使用found_index参数
通过添加found_index参数,可以按索引选择特定的控件。
# 查找MenuBar并使用索引 menu = win.child_window(control_type="MenuBar", found_index=0) print(menu.window_text())
索引从0开始计数。
© 版权声明
文章版权归作者所有,未经允许请勿转载。
THE END