【问题标题】:Access a Button in Toolbar Pywinauto访问工具栏中的按钮 Pywinauto
【发布时间】:2022-01-27 17:12:46
【问题描述】:

我正在尝试自动化我的一些办公室工作,并且是 pywinauto 的新手。 通过运行 print_control_identifiers() 我在我的应用程序中得到了以下信息。例如,如何访问并单击工具栏下的“按钮 - '打开项目文件'”?

已编辑:

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

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']
   | 
   | ...

【问题讨论】:

  • 最好也提供代码片段。如果您希望将来在 StackOverflow 上获得帮助,将正确答案标记为已接受也是一个好习惯(多年来,您的问题下没有任何已接受的答案!)。答案左侧的灰色复选框应该适合您。 ;-)

标签: ui-automation pywinauto


【解决方案1】:

假设您的顶级窗口规范名为dialog。从print_control_identifiers()/dump_tree() 输出中复制粘贴代码很容易:

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

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

【讨论】:

  • 谢谢,瓦西里。使用您的方法,我如何访问MenuItem - '' (L859, T321, R904, B366) | | ['MenuItem3'] 而不是按钮本身?此外,从一个运行到另一个短代码 - 运行时间发生了巨大变化,我发现自己在代码的第 2 行到第 3 行之间等待了将近一分钟。为什么会这样以及如何处理?
  • 我认为main_dlg.ToolbarOpenProjectFile.MenuItem3.select() 应该可以工作。或.invoke().expand().collapse()。只需调用.wrapper_object() 并执行dir(wrapper_object) 即可列出返回包装器的所有可用方法。生产代码.wrapper_object()可以省略。
  • 据我从上面的 print_control_identifiers() 层次结构中了解,MenuItem3 不是 ToolbarOpenProjectFile 的子项。如果是这样,怎么还能点击呢?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2023-03-12
  • 1970-01-01
  • 2015-07-10
  • 1970-01-01
  • 1970-01-01
  • 2011-07-15
  • 2021-11-01
相关资源
最近更新 更多