当前位置:   article > 正文

Pywinauto 中文文档入门指南_pywinauto 中文教程

pywinauto 中文教程

一旦你安装了pywinauto - 你怎么样? 第一个必要的事情是确定哪种可访问性技术(pywinauto的backend)可以用于您的应用程序。

Windows上受支持的辅助功能技术列表:

  • Win32 API (backend="win32") - 现在的默认backend

    • MFC, VB6, VCL, 简单的WinForms控件和大多数旧的遗留应用程序
  • MS UI Automation (backend="uia")

    • WinForms, WPF, Store apps, Qt5, 浏览器

    注意: Chrome在启动之前需要--force-renderer-accessibility cmd标志。 由于comtypes Python库限制,不支持自定义属性和控件。

到目前为止,Linux上的AT SPI和Apple Accessibility API都是长期计划。

GUI对象检查/Spy工具

如果您仍然不确定哪个backend最适合您,请尝试使用免费提供的对象检查/Spy工具:从GitHub repo gui-inspect-tool下载它们.

  • Spy++ 包含在MS Visual Studio发行版(甚至是Express或Community)中,可通过“开始”菜单访问。 它使用Win32 API。 这意味着如果Spy ++能够显示所有控件,那么“win32”`backend就是你需要的。 AutoIt Window Info工具是一种Spy ++克隆。

  • Inspect.exe 是Microsoft创建的另一个很棒的工具。 它包含在Windows SDK中,因此可以在x64 Windows上的以下位置找到它:

    C:\Program Files (x86)\Windows Kits\<winver>\bin\x64     
    

    将Inspect.exe切换到UIA mode(使用MS UI Automation)。 如果它可以显示比Spy ++更多的控件及其属性,那么可能是 "uia"backend是你的选择。

  • py_inspect 是基于pywinauto的多后端间谍工具的原型。 在可用后端之间切换可以通过“win32”和“uia”后端向您显示层次结构的差异。 py \ _inspectSWAPY的未来替代品,仅在pywinauto == 0.5.4出现时支持“win32”后端。 由于现代pywinauto 0.6.0+架构,py \ _insins的初始实现仅包含大约150行代码。

如果所有检测工具都看不到某些或所有控件,则仍然可以通过使用基本模块鼠标和键盘生成鼠标和键盘事件来控制应用程序。

自动化的切入点

所以你有一个应用程序,你知道它支持上面提到的一种可访问性技术。 下一步是什么?

首先,您应该启动应用程序或连接到现有的应用程序实例。 它可以使用Application对象完成。 这不仅仅是subprocess.Popen的克隆,而是进一步自动化的入口点,通过进程边界限制所有范围。 控制可能很少的应用程序实例很有用(您使用一个不打扰另一个实例的实例)。

  1. from pywinauto.application import Application
  2. app = Application(backend="uia").start('notepad.exe')
  3. # 描述Notepad.exe进程内的窗口
  4. dlg_spec = app.UntitledNotepad
  5. # 等到窗户真的开着
  6. actionable_dlg = dlg_spec.wait('visible')

如果你想要跨越流程边界(比如Win10计算器令人惊讶地在多个流程中绘制它的小部件),你的入口点是一个Desktop对象。

  1. from subprocess import Popen
  2. from pywinauto import Desktop
  3. Popen('calc.exe', shell=True)
  4. dlg = Desktop(backend="uia").Calculator
  5. dlg.wait('visible')

ApplicationDesktop对象都是backend特定的。 无需在后续操作中明确使用backend名称。

窗口规格

这是高级pywinauto API的核心概念。 您可以近似或更详细地描述任何窗口或控件,即使它尚不存在或已经关闭。 窗口规范还保留有关将用于获得真实窗口或控件的匹配/搜索算法的信息。

让我们创建一个详细的窗口规范:

  1. >>> dlg_spec = app.window(title=u'无标题 - 记事本')
  2. >>> dlg_spec
  3. <pywinauto.application.WindowSpecification object at 0x0568B790>
  4. >>> dlg_spec.wrapper_object()
  5. <pywinauto.controls.win32_controls.DialogWrapper object at 0x05639B70>

实际窗口查找由wrapper_object()方法执行。 它返回实际现有窗口/控件的一些包装器或引发ElementNotFoundError。 这个包装器可以通过发送动作或检索数据来处理窗口/控件。

但是Python可以隐藏这个wrapper_object()调用,这样你就可以在生产中拥有更紧凑的代码。 以下陈述完全相同:

  1. dlg_spec.wrapper_object().minimize() # 在调试时
  2. dlg_spec.minimize() # 在生产环境中

创建窗口规范有许多可能的标准。 这只是几个例子。

  1. # 可以是多层次的
  2. app.window(title_re='.* - Notepad$').window(class_name='Edit')
  3. # 可以结合标准
  4. dlg = Desktop(backend="uia").Calculator
  5. dlg.window(auto_id='num8Button', control_type='Button')

可以在pywinauto.findwindows.find_elements()函数中找到可能的标准列表。

属性解析魔法

Python通过动态解析对象属性简化了创建窗口规范。 但是一个属性名称与任何变量名称具有相同的限制:没有空格,逗号和其他特殊符号。 但幸运的是pywinauto使用“最佳匹配”算法来查找拼写错误和小变化。

  1. app.UntitledNotepad
  2. # 相当于
  3. app.window(best_match='UntitledNotepad')

通过类似字典的项目访问,可以使用Unicode字符和特殊符号。

  1. app['Untitled - Notepad']
  2. # 是相同的
  3. app.window(best_match='Untitled - Notepad')

如何知道魔法属性名称

如何将“最佳匹配”金牌附加到控件上有几个原则。 因此,如果窗口规范接近其中一个名称,您将获得成功的名称匹配。

  1. 按标题(窗口文字,名称): app.Properties.OK.click()
  2. 按标题和控件类型: app.Properties.OKButton.click()
  3. 按控件类型和编号: app.Properties.Button3.click() (注意: Button0和Button1匹配相同的按钮,Button2是下一个,等等。)
  4. 按左上角标签和控件类型: app.OpenDialog.FileNameEdit.set_text("")
  5. 按控件类型和项目文本:app.Properties.TabControlSharing.select("General")

通常并非所有这些匹配的名称都可以同时使用。 要检查指定对话框的这些名称,可以使用print_control_identifiers()方法。 可能的“best_match”名称显示为树中每个控件的Python列表。 也可以从方法输出中复制更详细的窗口规范。 比方说 app.Properties.child_window(title="Contains:", auto_id="13087", control_type="Edit")

  1. >>> app.Properties.print_control_identifiers()
  2. Control Identifiers:
  3. Dialog - 'Windows NT Properties' (L688, T518, R1065, B1006)
  4. [u'Windows NT PropertiesDialog', u'Dialog', u'Windows NT Properties']
  5. child_window(title="Windows NT Properties", control_type="Window")
  6. |
  7. | Image - '' (L717, T589, R749, B622)
  8. | [u'', u'0', u'Image1', u'Image0', 'Image', u'1']
  9. | child_window(auto_id="13057", control_type="Image")
  10. |
  11. | Image - '' (L717, T630, R1035, B632)
  12. | ['Image2', u'2']
  13. | child_window(auto_id="13095", control_type="Image")
  14. |
  15. | Edit - 'Folder name:' (L790, T596, R1036, B619)
  16. | [u'3', 'Edit', u'Edit1', u'Edit0']
  17. | child_window(title="Folder name:", auto_id="13156", control_type="Edit")
  18. |
  19. | Static - 'Type:' (L717, T643, R780, B658)
  20. | [u'Type:Static', u'Static', u'Static1', u'Static0', u'Type:']
  21. | child_window(title="Type:", auto_id="13080", control_type="Text")
  22. |
  23. | Edit - 'Type:' (L790, T643, R1036, B666)
  24. | [u'4', 'Edit2', u'Type:Edit']
  25. | child_window(title="Type:", auto_id="13059", control_type="Edit")
  26. |
  27. | Static - 'Location:' (L717, T669, R780, B684)
  28. | [u'Location:Static', u'Location:', u'Static2']
  29. | child_window(title="Location:", auto_id="13089", control_type="Text")
  30. |
  31. | Edit - 'Location:' (L790, T669, R1036, B692)
  32. | ['Edit3', u'Location:Edit', u'5']
  33. | child_window(title="Location:", auto_id="13065", control_type="Edit")
  34. |
  35. | Static - 'Size:' (L717, T695, R780, B710)
  36. | [u'Size:Static', u'Size:', u'Static3']
  37. | child_window(title="Size:", auto_id="13081", control_type="Text")
  38. |
  39. | Edit - 'Size:' (L790, T695, R1036, B718)
  40. | ['Edit4', u'6', u'Size:Edit']
  41. | child_window(title="Size:", auto_id="13064", control_type="Edit")
  42. |
  43. | Static - 'Size on disk:' (L717, T721, R780, B736)
  44. | [u'Size on disk:', u'Size on disk:Static', u'Static4']
  45. | child_window(title="Size on disk:", auto_id="13107", control_type="Text")
  46. |
  47. | Edit - 'Size on disk:' (L790, T721, R1036, B744)
  48. | ['Edit5', u'7', u'Size on disk:Edit']
  49. | child_window(title="Size on disk:", auto_id="13106", control_type="Edit")
  50. |
  51. | Static - 'Contains:' (L717, T747, R780, B762)
  52. | [u'Contains:1', u'Contains:0', u'Contains:Static', u'Static5', u'Contains:']
  53. | child_window(title="Contains:", auto_id="13088", control_type="Text")
  54. |
  55. | Edit - 'Contains:' (L790, T747, R1036, B770)
  56. | [u'8', 'Edit6', u'Contains:Edit']
  57. | child_window(title="Contains:", auto_id="13087", control_type="Edit")
  58. |
  59. | Image - 'Contains:' (L717, T773, R1035, B775)
  60. | [u'Contains:Image', 'Image3', u'Contains:2']
  61. | child_window(title="Contains:", auto_id="13096", control_type="Image")
  62. |
  63. | Static - 'Created:' (L717, T786, R780, B801)
  64. | [u'Created:', u'Created:Static', u'Static6', u'Created:1', u'Created:0']
  65. | child_window(title="Created:", auto_id="13092", control_type="Text")
  66. |
  67. | Edit - 'Created:' (L790, T786, R1036, B809)
  68. | [u'Created:Edit', 'Edit7', u'9']
  69. | child_window(title="Created:", auto_id="13072", control_type="Edit")
  70. |
  71. | Image - 'Created:' (L717, T812, R1035, B814)
  72. | [u'Created:Image', 'Image4', u'Created:2']
  73. | child_window(title="Created:", auto_id="13097", control_type="Image")
  74. |
  75. | Static - 'Attributes:' (L717, T825, R780, B840)
  76. | [u'Attributes:Static', u'Static7', u'Attributes:']
  77. | child_window(title="Attributes:", auto_id="13091", control_type="Text")
  78. |
  79. | CheckBox - 'Read-only (Only applies to files in folder)' (L790, T825, R1035, B841)
  80. | [u'CheckBox0', u'CheckBox1', 'CheckBox', u'Read-only (Only applies to files in folder)CheckBox', u'Read-only (Only applies to files in folder)']
  81. | child_window(title="Read-only (Only applies to files in folder)", auto_id="13075", control_type="CheckBox")
  82. |
  83. | CheckBox - 'Hidden' (L790, T848, R865, B864)
  84. | ['CheckBox2', u'HiddenCheckBox', u'Hidden']
  85. | child_window(title="Hidden", auto_id="13076", control_type="CheckBox")
  86. |
  87. | Button - 'Advanced...' (L930, T845, R1035, B868)
  88. | [u'Advanced...', u'Advanced...Button', 'Button', u'Button1', u'Button0']
  89. | child_window(title="Advanced...", auto_id="13154", control_type="Button")
  90. |
  91. | Button - 'OK' (L814, T968, R889, B991)
  92. | ['Button2', u'OK', u'OKButton']
  93. | child_window(title="OK", auto_id="1", control_type="Button")
  94. |
  95. | Button - 'Cancel' (L895, T968, R970, B991)
  96. | ['Button3', u'CancelButton', u'Cancel']
  97. | child_window(title="Cancel", auto_id="2", control_type="Button")
  98. |
  99. | Button - 'Apply' (L976, T968, R1051, B991)
  100. | ['Button4', u'ApplyButton', u'Apply']
  101. | child_window(title="Apply", auto_id="12321", control_type="Button")
  102. |
  103. | TabControl - '' (L702, T556, R1051, B962)
  104. | [u'10', u'TabControlSharing', u'TabControlPrevious Versions', u'TabControlSecurity', u'TabControl', u'TabControlCustomize']
  105. | child_window(auto_id="12320", control_type="Tab")
  106. | |
  107. | | TabItem - 'General' (L704, T558, R753, B576)
  108. | | [u'GeneralTabItem', 'TabItem', u'General', u'TabItem0', u'TabItem1']
  109. | | child_window(title="General", control_type="TabItem")
  110. | |
  111. | | TabItem - 'Sharing' (L753, T558, R801, B576)
  112. | | [u'Sharing', u'SharingTabItem', 'TabItem2']
  113. | | child_window(title="Sharing", control_type="TabItem")
  114. | |
  115. | | TabItem - 'Security' (L801, T558, R851, B576)
  116. | | [u'Security', 'TabItem3', u'SecurityTabItem']
  117. | | child_window(title="Security", control_type="TabItem")
  118. | |
  119. | | TabItem - 'Previous Versions' (L851, T558, R947, B576)
  120. | | [u'Previous VersionsTabItem', u'Previous Versions', 'TabItem4']
  121. | | child_window(title="Previous Versions", control_type="TabItem")
  122. | |
  123. | | TabItem - 'Customize' (L947, T558, R1007, B576)
  124. | | [u'CustomizeTabItem', 'TabItem5', u'Customize']
  125. | | child_window(title="Customize", control_type="TabItem")
  126. |
  127. | TitleBar - 'None' (L712, T521, R1057, B549)
  128. | ['TitleBar', u'11']
  129. | |
  130. | | Menu - 'System' (L696, T526, R718, B548)
  131. | | [u'System0', u'System', u'System1', u'Menu', u'SystemMenu']
  132. | | child_window(title="System", auto_id="MenuBar", control_type="MenuBar")
  133. | | |
  134. | | | MenuItem - 'System' (L696, T526, R718, B548)
  135. | | | [u'System2', u'MenuItem', u'SystemMenuItem']
  136. | | | child_window(title="System", control_type="MenuItem")
  137. | |
  138. | | Button - 'Close' (L1024, T519, R1058, B549)
  139. | | [u'CloseButton', u'Close', 'Button5']
  140. | | child_window(title="Close", control_type="Button")

看看这些例子

包括以下示例: 注意: 示例取决于语言 - 它们仅适用于为其编程的产品语言。 除非突出显示,否则所有示例都已编程为英语软件。

  • mspaint.py 控制画图
  • notepad_fast.py 使用快速计时设置来控制记事本
  • notepad_slow.py 使用慢速计时设置来控制记事本
  • notepad_item.py 使用item而不是属性访问来控制记事本。
  • misc_examples.py 显示一些例外以及如何获取控件标识符。
  • save_from_internet_explorer.py 从Internet Explorer保存网页。
  • save_from_firefox.py 从Firefox保存网页。
  • get_winrar_info.py 如何进行多语言自动化的示例。 这不是一个理想的例子(适用于法语,捷克语和德语WinRar)
  • forte_agent_sample.py 处理复杂应用程序的示例,该应用程序非常动态,并且在启动时经常提供不同的对话框。
  • windowmediaplayer.py 只是另一个例子 - 处理ListView中的复选框。
  • test_sakura.pytest_sakura2.py 两个自动化日本产品的例子。

在命令行自动记事本

请在下面找到一个示例运行

  1. C:\>python
  2. Python 2.4.2 (#67, Sep 28 2005, 12:41:11) [MSC v.1310 32 bit (Intel)] on win32
  3. Type "help", "copyright", "credits" or "license" for more information.
  4. (1) >>> from pywinauto import application
  5. (2) >>> app = application.Application()
  6. (3) >>> app.start("Notepad.exe")
  7. <pywinauto.application.Application object at 0x00AE0990>
  8. (4) >>> app.UntitledNotepad.draw_outline()
  9. (5) >>> app.UntitledNotepad.menu_select("Edit -> Replace")
  10. (6) >>> app.Replace.print_control_identifiers()
  11. Control Identifiers:
  12. Dialog - 'Replace' (L179, T174, R657, B409)
  13. ['ReplaceDialog', 'Dialog', 'Replace']
  14. child_window(title="Replace", class_name="#32770")
  15. |
  16. | Static - 'Fi&nd what:' (L196, T230, R292, B246)
  17. | ['Fi&nd what:Static', 'Fi&nd what:', 'Static', 'Static0', 'Static1']
  18. | child_window(title="Fi&nd what:", class_name="Static")
  19. |
  20. | Edit - '' (L296, T226, R524, B250)
  21. | ['Fi&nd what:Edit', 'Edit', 'Edit0', 'Edit1']
  22. | child_window(class_name="Edit")
  23. |
  24. | Static - 'Re&place with:' (L196, T264, R292, B280)
  25. | ['Re&place with:', 'Re&place with:Static', 'Static2']
  26. | child_window(title="Re&place with:", class_name="Static")
  27. |
  28. | Edit - '' (L296, T260, R524, B284)
  29. | ['Edit2', 'Re&place with:Edit']
  30. | child_window(class_name="Edit")
  31. |
  32. | CheckBox - 'Match &whole word only' (L198, T304, R406, B328)
  33. | ['CheckBox', 'Match &whole word onlyCheckBox', 'Match &whole word only', 'CheckBox0', 'CheckBox1']
  34. | child_window(title="Match &whole word only", class_name="Button")
  35. |
  36. | CheckBox - 'Match &case' (L198, T336, R316, B360)
  37. | ['CheckBox2', 'Match &case', 'Match &caseCheckBox']
  38. | child_window(title="Match &case", class_name="Button")
  39. |
  40. | Button - '&Find Next' (L536, T220, R636, B248)
  41. | ['&Find Next', '&Find NextButton', 'Button', 'Button0', 'Button1']
  42. | child_window(title="&Find Next", class_name="Button")
  43. |
  44. | Button - '&Replace' (L536, T254, R636, B282)
  45. | ['&ReplaceButton', '&Replace', 'Button2']
  46. | child_window(title="&Replace", class_name="Button")
  47. |
  48. | Button - 'Replace &All' (L536, T288, R636, B316)
  49. | ['Replace &AllButton', 'Replace &All', 'Button3']
  50. | child_window(title="Replace &All", class_name="Button")
  51. |
  52. | Button - 'Cancel' (L536, T322, R636, B350)
  53. | ['CancelButton', 'Cancel', 'Button4']
  54. | child_window(title="Cancel", class_name="Button")
  55. |
  56. | Button - '&Help' (L536, T362, R636, B390)
  57. | ['&Help', '&HelpButton', 'Button5']
  58. | child_window(title="&Help", class_name="Button")
  59. |
  60. | Static - '' (L196, T364, R198, B366)
  61. | ['ReplaceStatic', 'Static3']
  62. | child_window(class_name="Static")
  63. (7) >>> app.Replace.Cancel.click()
  64. (8) >>> app.UntitledNotepad.Edit.type_keys("Hi from Python interactive prompt %s" % str(dir()), with_spaces = True)
  65. <pywinauto.controls.win32_controls.EditWrapper object at 0x00DDC2D0>
  66. (9) >>> app.UntitledNotepad.menu_select("File -> Exit")
  67. (10) >>> app.Notepad.DontSave.click()
  68. >>>
  1. 导入pywinauto.application模块(通常是您需要直接导入的唯一模块)

  2. 创建一个Application实例。 对该应用程序的所有访问都是通过该对象完成的。

  3. 我们在步骤2中创建了一个Application实例,但是我们没有提供它所引用的Windows应用程序的任何信息。 通过使用start()方法,我们执行该应用程序并将其连接到Application实例应用程序。

  4. 在记事本对话框周围绘制一个绿色矩形 - 这样我们就知道我们有正确的窗口。

  5. 从应用程序所连接的应用程序的“记事本”对话框的“编辑”菜单中选择“替换”项。 此操作将显示“替换”对话框。

  6. 在“替换”对话框上打印控件的标识符,例如,“替换”对话框上的第一个编辑控件可以通过以下任何标识符引用:

    1. app.Replace.Edit
    2. app.Replace.Edit0
    3. app.Replace.Edit1
    4. app.FindwhatEdit

    最后一个是让用户在阅读脚本后最好了解脚本的功能。

  7. 关闭“替换”对话框。 (在脚本文件中,使用close_click()而不是click()更安全,因为close_click()会等待一段时间以给窗口时间关闭对话框。)

  8. 让我们在记事本文本区域中键入一些文本。 没有with_spaces参数,就不会输入空格。 请参阅SendKeys的文档,了解此方法,因为它是SendKeys的一个薄包装器。

  9. 要求退出记事本

  10. 我们将被询问是否要保存 - 单击“否”按钮。

本文内容由网友自发贡献,转载请注明出处:https://www.wpsshop.cn/w/我家小花儿/article/detail/1016885
推荐阅读
  

闽ICP备14008679号