我想使用 pywinauto 自动化 Windows 应用程序,但我在选择菜单栏选项时遇到了困难。当我使用 menu_select 函数时,我收到 AttributeError 。
Python 3.11.5 pywinauto 0.6.8
代码:
from pywinauto.application import Application
app = Application(backend="uia")
app.start(app_path)
dlg = app.window()
dlg.wait("visible")
dlg.print_control_identifiers()
dlg.menu_select("File->Exit")
输出:
Control Identifiers:
Dialog - 'x' (L234, T234, R1242, B989)
['xDialog', 'x', 'Dialog']
child_window(title="x", auto_id="MainForm", control_type="Window")
| GroupBox - 'Dock Top' (L242, T265, R1234, B289)
| ['Dock Top', 'GroupBox', 'Dock TopGroupBox']
| child_window(title="Dock Top", auto_id="3342822", control_type="Group")
| |
| | Menu - 'Main Menu' (L242, T265, R1234, B287)
| | ['Main Menu', 'Menu', 'Main MenuMenu', 'Menu0', 'Menu1']
| | child_window(title="Main Menu", auto_id="590138", control_type="MenuBar")
| | |
| | | MenuItem - 'File' (L250, T266, R283, B286)
| | | ['File', 'MenuItem', 'FileMenuItem', 'MenuItem0', 'MenuItem1']
| | | child_window(title="File", control_type="MenuItem")
| | | |
| | | | Button - 'Hardcopy' (L0, T0, R0, B0)
| | | | ['Hardcopy', 'Button', 'HardcopyButton', 'Button0', 'Button1']
| | | | child_window(title="Hardcopy", control_type="Button")
| | | |
| | | | Button - 'Exit' (L0, T0, R0, B0)
| | | | ['Exit', 'Button2', 'ExitButton']
| | | | child_window(title="Exit", control_type="Button")
| | |
[...]
Traceback (most recent call last):
File "C:\Users\jalo\Desktop\auto.py", line 27, in <module>
dlg.menu_select("File->Exit")
File "C:\Users\jalo\AppData\Local\Programs\Python\Python311\Lib\site-packages\pywinauto\controls\uiawrapper.py", line 721, in menu_select
raise AttributeError
AttributeError
我也尝试过解决方法
dlg.File.select() # or .click_input()
dlg.Exit.click()
并选择展开文件选项列表,但由于
单击失败Traceback (most recent call last):
File "C:\Users\jalo\Desktop\auto.py", line 32, in <module>
dlg.Exit.click()
File "C:\Users\jalo\AppData\Local\Programs\Python\Python311\Lib\site-packages\pywinauto\application.py", line 379, in __getattribute__
ctrls = self.__resolve_control(self.criteria)
File "C:\Users\jalo\AppData\Local\Programs\Python\Python311\Lib\site-packages\pywinauto\application.py", line 250, in __resolve_control
ctrl = wait_until_passes(
File "C:\Users\jalo\AppData\Local\Programs\Python\Python311\Lib\site-packages\pywinauto\timings.py", line 436, in wait_until_passes
func_val = func(*args, **kwargs)
File "C:\Users\jalo\AppData\Local\Programs\Python\Python311\Lib\site-packages\pywinauto\application.py", line 203, in __get_ctrl
dialog = self.backend.generic_wrapper_class(findwindows.find_element(**criteria[0]))
File "C:\Users\jalo\AppData\Local\Programs\Python\Python311\Lib\site-packages\pywinauto\findwindows.py", line 98, in find_element
raise exception
pywinauto.findwindows.ElementAmbiguousError: There are 2 elements that match the criteria {'backend': 'uia', 'process': 4988}
您是否尝试过从主菜单级别进行选择?
dlg.menu_select("Main Menu->File->Exit")
试试这个,因为你有 2 个对话框 `
dlg = app.window()
dlg.wait("ready")
dlg.File.select()
dlg1 = app.window()
dlg1.Exit.select()
`