windows 桌面GUI自动化- 14.pywinauto 找到多个相同控件使用found_index
  iKXeBmqdRgYF 2023年11月05日 71 0

前言

pywinauto 在查找到多个相同控件时操作会报错,可以使用found_index 选择其中的一个

查找到多个

查找control_type="MenuBar" 的所有控件

from pywinauto import Application


app = Application('uia').start("notepad.exe")
win = app.window(title_re="无标题 - 记事本")
# 输入内容
win.child_window(title="文本编辑器").set_text("hello world")

# 查找MenuBar
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, 460858>, 'backend': 'uia'}

加上 found_index 参数,按索引取值

# 查找
menu = win.child_window(control_type="MenuBar", found_index=0)
print(menu.window_text())

索引下标从0开始



【版权声明】本文内容来自摩杜云社区用户原创、第三方投稿、转载,内容版权归原作者所有。本网站的目的在于传递更多信息,不拥有版权,亦不承担相应法律责任。如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@moduyun.com

  1. 分享:
最后一次编辑于 2023年11月08日 0

暂无评论

推荐阅读
iKXeBmqdRgYF