访问工具栏 Pywinauto 中的按钮

Access a Button in Toolbar Pywinauto

我正在尝试使我的一些办公室工作自动化,并且是 pywinauto 的新手。 通过 运行 print_control_identifiers() 我在我的应用程序中得到了以下内容。例如,我怎样才能访问并单击工具栏下的“按钮 - 'Open Project File'”?

已编辑:

@Vasily Ryabov - 感谢您的评论。我在下面添加了一些代码片段(注释行也适用于我)

app = Application(backend="uia").start(r"C:\Program Files\Leica Geosystems\TruView\TruView.exe", timeout=5)
main_dlg = app['TruView']
main_dlg.print_control_identifiers()
# main_dlg["Open Project File"].click_input()
main_dlg.child_window(title="Open Project File", control_type="Button").click_input()
Dialog - 'TruView'    (L846, T0, R1895, B1038)
['Dialog', 'TruView', 'TruViewDialog']
child_window(title="TruView", control_type="Window")
   | 
   | Toolbar - ''    (L855, T38, R908, B1001)
   | ['ToolbarOpen Project File', 'ToolbarSaved Clips', 'Toolbar', 'ToolbarSave Project File', 'ToolbarSiteMaps', 'Toolbar0', 'Toolbar1']
   |    | 
   |    | MenuItem - ''    (L859, T460, R904, B505)
   |    | ['MenuItem', 'MenuItem0', 'MenuItem1']
   |    | 
   |    | MenuItem - ''    (L859, T413, R904, B458)
   |    | ['MenuItem2']
   |    | 
   |    | Button - 'Save Project File'    (L859, T367, R904, B412)
   |    | ['Save Project FileButton', 'Save Project File', 'Button', 'Button0', 'Button1']
   |    | child_window(title="Save Project File", control_type="Button")
   |    | 
   |    | MenuItem - ''    (L859, T321, R904, B366)
   |    | ['MenuItem3']
   |    | 
   |    | MenuItem - ''    (L859, T274, R904, B319)
   |    | ['MenuItem4']
   |    | 
   |    | MenuItem - 'Saved Clips'    (L859, T228, R904, B273)
   |    | ['Saved ClipsMenuItem', 'Saved Clips', 'MenuItem5']
   |    | child_window(title="Saved Clips", control_type="MenuItem")
   |    | 
   |    | MenuItem - ''    (L859, T182, R904, B227)
   |    | ['MenuItem6']
   |    | 
   |    | Button - ''    (L859, T134, R904, B179)
   |    | ['Button2']
   |    | 
   |    | Button - 'SiteMaps'    (L859, T88, R904, B133)
   |    | ['SiteMaps', 'SiteMapsButton', 'Button3']
   |    | child_window(title="SiteMaps", control_type="Button")
   |    | 
   |    | Button - 'Open Project File'    (L859, T42, R904, B87)
   |    | ['Open Project File', 'Open Project FileButton', 'Button4']
   |    | child_window(title="Open Project File", control_type="Button")
   | 
   | StatusBar - ''    (L855, T1001, R1886, B1029)
   | ['StatusBar']
   |    | 
   |    | Thumb - ''    (L1869, T1012, R1886, B1029)
   |    | ['Thumb']
   |    | 
   |    | Static - ''    (L857, T1004, R1107, B1027)
   |    | ['Static', 'Static0', 'Static1']
   |    | 
   |    | Static - ''    (L1113, T1004, R1433, B1027)
   |    | ['Static2']
   |    | 
   |    | Static - ''    (L1439, T1004, R1639, B1027)
   |    | ['Static3']
   |    | 
   |    | Static - ''    (L1645, T1004, R1845, B1027)
   |    | ['Static4']
   | 
   | ...

让我们猜猜您的顶级 window 规格被命名为 dialog。从 print_control_identifiers()/dump_tree() 输出 copy-paste 代码很容易:

main_dlg.child_window(title="Open Project File", control_type="Button").click()

如果 .click() 引发 NoPatternInterfaceError,请尝试 .click_input().toggle()