为了账号安全,请及时绑定邮箱和手机立即绑定

如何解决 pywinauto 的 ElementAmbigouslyError:

如何解决 pywinauto 的 ElementAmbigouslyError:

牛魔王的故事 2023-06-27 17:59:57
我正在尝试使用 python 和 pywinauto 自动运行Rufus。到目前为止,我已经能够运行可执行文件并修改 Rufus 主屏幕上的控件。在我编写的代码单击“开始”按钮后,Rufus 将显示一个弹出窗口。此弹出窗口警告您 USB 闪存盘的全部内容将被删除。我无法做的是连接到该弹出窗口并按那里的“确定”按钮。这是我写的代码:# Connect to an existing window with title matching a regular expressiondef ConnectWindow(exp):  # Look for window matching regular expression  try:    handles = pwa.findwindows.find_windows(found_index=0, title_re=exp)  except pwa.findwindows.WindowNotFoundError:    handles = None  # Make sure something was found  if handles:    # Make sure only one found    if len(handles) > 1:      print('Matched more than one window matching regular expression: {0}'.format(exp))      sys.exit(1)    # Return it!    return pwa.Application().connect(handle=handles[0]).window()  # Nothing found  return Nonepopup = ConnectWindow('^Rufus$')popup.set_focus()pwa.keyboard.send_keys('{RIGHT}{ENTER}')ConnectWindow 确实找到了窗口,并且弹出窗口的类型为 pywinauto.application.WindowSpecification。但是,每当我尝试对弹出窗口执行任何操作(例如 set_focus)时,我都会收到以下错误:pywinauto.findwindows.ElementAmbigouslyError: There are 2 elements that match the criteria {'backend': 'win32', 'process': 32992}有谁知道我该如何解决这个问题?
查看完整描述

1 回答

?
萧十郎

TA贡献1815条经验 获得超12个赞

所有这些代码都可以写得更加紧凑。请不要find_windows直接使用函数。阅读入门指南以了解 WindowSpecification 的工作原理。


app = pwa.Application(backend="win32").connect(found_index=0, title_re=exp, timeout=10)

popup = app.window(found_index=0, title_re=exp)


popup.type_keys('{RIGHT}{ENTER}') # it calls .set_focus() inside


查看完整回答
反对 回复 2023-06-27
  • 1 回答
  • 0 关注
  • 124 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信