上篇实现了如何打开一个PC端应用程序,接下来就要操作应用窗口了
一、查看pywinauto帮助文档
上篇文章中也有写到pywinauto官网地址,查看pywinauto的帮助文档:https://pywinauto.readthedocs.io/en/latest/code/pywinauto.application.html
从文档中可以查看到操作窗口空间的相关方法和示例;
二、操作实例
1、测试的PC端应用程序
输出代码
2、通过window.print_control_identifiers() # 打印'标识符',将控件及其后代的标识符打印到深度深度
from pywinauto import Application
import time
app = Application(backend="uia").start(r"D:\tools\zhanxunpingtai\UPGRADEDOWNLOAD_R21.0.0001\
UPGRADEDOWNLOAD_R21.0.0001\Bin\UpgradeDownload.exe")
"""判断是否有弹框"""
while not app.top_window():
time.sleep(3)
"""检查指定对话框的名称"""
window = app.window(title="UpgradeDownload - R21.0.0001")
window.print_control_identifiers() # 打印'标识符',将控件及其后代的标识符打印到深度深度
time.sleep(2)
运行上面的代码,输出如下结果
D:\python\python3.6.1\python36.exe D:/work/test/2019_07_25/python_PC.py
Control Identifiers:
Dialog - 'UpgradeDownload - R21.0.0001' (L200, T200, R1640, B960)
['UpgradeDownload - R21.0.0001', 'UpgradeDownload - R21.0.0001Dialog', 'Dialog', 'Dialog0', 'Dialog1']
child_window(title="UpgradeDownload - R21.0.0001", control_type="Window")
|
| Dialog - 'Download' (L677, T475, R1163, B686)
| ['Download', 'DownloadDialog', 'Dialog2']
| child_window(title="Download", control_type="Window")
| |
| | Button - '是(Y)' (L965, T640, R1053, B670)
| | ['是(Y)Button', 'Button', '是(Y)', 'Button0', 'Button1']
| | child_window(title="是(Y)", auto_id="6", control_type="Button")
| |
| | Button - '否(N)' (L1063, T640, R1151, B670)
| | ['否(N)', 'Button2', '否(N)Button']
| | child_window(title="否(N)", auto_id="7", control_type="Button")
| |
| | Image - '' (L705, T530, R737, B562)
| | ['', 'Image', '0', '1']
| | child_window(auto_id="20", control_type="Image")
| |
| | Static - 'Load latest pac file?\n\n\\10.0.3.206\share_nxos\ROM\Debug\MS18\YZ-ZX\V1.0.0_2019.07.29_11.26.04\YZ-ZX_ms18_V1.0.0_pac\YZ-ZX_ms18_V1.0.0.pac' (L745, T530, R1123, B600)
| | ['Static', 'Load latest pac file?\n\n\\\\10.0.3.206\\share_nxos\\ROM\\Debug\\MS18\\YZ-ZX\\V1.0.0_2019.07.29_11.26.04\\YZ-ZX_ms18_V1.0.0_pac\\YZ-ZX_ms18_V1.0.0.pacStatic', 'Load latest pac file?\n\n\\\\10.0.3.206\\share_nxos\\ROM\\Debug\\MS18\\YZ-ZX\\V1.0.0_2019.07.29_11.26.04\\YZ-ZX_ms18_V1.0.0_pac\\YZ-ZX_ms18_V1.0.0.pac', 'Static0', 'Static1']
| | child_window(title="Load latest pac file?\n\n\\10.0.3.206\share_nxos\ROM\Debug\MS18\YZ-ZX\V1.0.0_2019.07.29_11.26.04\YZ-ZX_ms18_V1.0.0_pac\YZ-ZX_ms18_V1.0.0.pac", auto_id="65535", control_type="Text")
| |
| | TitleBar - '' (L680, T478, R1160, B500)
| | ['2', 'TitleBar', 'TitleBar0', 'TitleBar1']
| | |
| | | Button - '关闭' (L1113, T471, R1162, B491)
| | | ['关闭', '关闭Button', 'Button3', '关闭0', '关闭1', '关闭Button0', '关闭Button1']
| | | child_window(title="关闭", control_type="Button")
|
| ListBox - '' (L208, T288, R1632, B933)
| ['ListBox', 'Load latest pac file?\n\n\\\\10.0.3.206\\share_nxos\\ROM\\Debug\\MS18\\YZ-ZX\\V1.0.0_2019.07.29_11.26.04\\YZ-ZX_ms18_V1.0.0_pac\\YZ-ZX_ms18_V1.0.0.pacListBox']
| child_window(auto_id="59648", control_type="List")
| |
| | Header - '' (L210, T290, R1630, B313)
| | ['3', 'Header']
| | child_window(auto_id="Header", control_type="Header")
| | |
| | | HeaderItem - 'Port' (L210, T290, R260, B313)
| | | ['Port', 'PortHeaderItem', 'HeaderItem', 'HeaderItem0', 'HeaderItem1']
| | | child_window(title="Port", control_type="HeaderItem")
| | |
| | | HeaderItem - 'Step' (L260, T290, R380, B313)
| | | ['Step', 'StepHeaderItem', 'HeaderItem2']
| | | child_window(title="Step", control_type="HeaderItem")
| | |
| | | HeaderItem - 'Status' (L380, T290, R560, B313)
| | | ['StatusHeaderItem', 'Status', 'HeaderItem3']
| | | child_window(title="Status", control_type="HeaderItem")
| | |
| | | HeaderItem - 'Progress' (L560, T290, R875, B313)
| | | ['ProgressHeaderItem', 'Progress', 'HeaderItem4']
| | | child_window(title="Progress", control_type="HeaderItem")
| | |
| | | HeaderItem - 'Time(s)' (L875, T290, R975, B313)
| | | ['Time(s)HeaderItem', 'Time(s)', 'HeaderItem5']
| | | child_window(title="Time(s)", control_type="HeaderItem")
| | |
| | | HeaderItem - 'MCP Type' (L975, T290, R1155, B313)
| | | ['MCP TypeHeaderItem', 'MCP Type', 'HeaderItem6']
| | | child_window(title="MCP Type", control_type="HeaderItem")
|
| Toolbar - '' (L208, T230, R1632, B288)
| ['4', 'ToolbarStart downloading', 'ToolbarAbout', 'ToolbarStop downloading', 'Toolbar', 'ToolbarLoad packet', 'ToolbarSettings']
| child_window(auto_id="59392", control_type="ToolBar")
| |
| | Static - '' (L508, T234, R1308, B284)
| | ['5', 'Static2']
| | child_window(auto_id="32777", control_type="Text")
| |
| | Button - 'Load packet' (L215, T232, R270, B286)
| | ['Button4', 'Load packet', 'Load packetButton']
| | child_window(title="Load packet", control_type="Button")
| |
| | Button - 'Settings' (L270, T232, R325, B286)
| | ['SettingsButton', 'Button5', 'Settings']
| | child_window(title="Settings", control_type="Button")
| |
| | Separator - '' (L325, T232, R331, B286)
| | ['6', 'Separator', 'Separator0', 'Separator1']
| |
| | Button - 'Start downloading' (L331, T232, R386, B286)
| | ['Start downloadingButton', 'Start downloading', 'Button6']
| | child_window(title="Start downloading", control_type="Button")
| |
| | Button - 'Stop downloading' (L386, T232, R441, B286)
| | ['Stop downloadingButton', 'Stop downloading', 'Button7']
| | child_window(title="Stop downloading", control_type="Button")
| |
| | Separator - '' (L441, T232, R447, B286)
| | ['7', 'Separator2']
| |
| | Button - 'About' (L447, T232, R502, B286)
| | ['About', 'AboutButton', 'Button8']
| | child_window(title="About", control_type="Button")
| |
| | Separator - '' (L502, T232, R508, B286)
| | ['8', 'Separator3']
| |
| | Separator - '' (L508, T232, R1308, B286)
| | ['9', 'Separator4']
|
| StatusBar - '' (L208, T933, R1632, B952)
| ['ReadyStatusBar', 'StatusBar']
| child_window(auto_id="59393", control_type="StatusBar")
| |
| | Static - 'Ready' (L208, T935, R1506, B952)
| | ['Static3', 'Ready', 'ReadyStatic', 'ReadyStatic0', 'ReadyStatic1']
| | child_window(title="Ready", control_type="Text")
| |
| | Static - '' (L1508, T935, R1537, B952)
| | ['Static4', 'ReadyStatic2']
| |
| | Static - 'NUM' (L1539, T935, R1576, B952)
| | ['Static5', 'NUM', 'NUMStatic', 'NUMStatic0', 'NUMStatic1']
| | child_window(title="NUM", control_type="Text")
| |
| | Static - '' (L1578, T935, R1613, B952)
| | ['Static6', 'NUMStatic2']
|
| TitleBar - '' (L224, T203, R1632, B230)
| ['10', 'TitleBar2']
| |
| | Menu - '系统' (L208, T208, R229, B229)
| | ['系统Menu', 'Menu', '系统', '系统0', '系统1']
| | child_window(title="系统", control_type="MenuBar")
| | |
| | | MenuItem - '系统' (L208, T208, R229, B229)
| | | ['系统MenuItem', 'MenuItem', '系统2']
| | | child_window(title="系统", control_type="MenuItem")
| |
| | Button - '最小化' (L1529, T201, R1558, B221)
| | ['Button9', '最小化', '最小化Button']
| | child_window(title="最小化", control_type="Button")
| |
| | Button - '最大化' (L1558, T201, R1585, B221)
| | ['最大化Button', '最大化', 'Button10']
| | child_window(title="最大化", control_type="Button")
| |
| | Button - '关闭' (L1585, T201, R1634, B221)
| | ['关闭2', '关闭Button2', 'Button11']
| | child_window(title="关闭", control_type="Button")
Process finished with exit code 0
3、操作控件
从结果中可以看到,上图红色框框中就是弹框的控件信息,接下来我们就点击‘否’,看实现代码
"""是否加载上一次路径控件信息,选择否"""
load_window = window.child_window(title="否(N)", auto_id="7", control_type="Button")
# 点击Button按钮 否
if load_window:
load_window.click()
这样就完成了点击‘否’操作,后续控件操作,也是按照以上步骤去操作即可(遇到问题记得先网上找找)
4、附上一些常用的特殊符号快捷键对应如下
SHIFT +
CTRL ^
ALT %
空格键 {SPACE}
BACKSPACE {BACKSPACE}、{BS} or {BKSP}
BREAK {BREAK}
CAPS LOCK {CAPSLOCK}
DEL or DELETE {DELETE} or {DEL}
DOWN ARROW {DOWN}
END {END}
ENTER {ENTER} or ~
ESC {ESC}
HELP {HELP}
HOME {HOME}
INS or INSERT {INSERT} or {INS}
LEFT ARROW {LEFT}
NUM LOCK {NUMLOCK}
PAGE DOWN {PGDN}
PAGE UP {PGUP}
PRINT SCREEN {PRTSC}
RIGHT ARROW {RIGHT}
SCROLL LOCK {SCROLLLOCK}
TAB {TAB}
UP ARROW {UP}
+ {ADD}
- {SUBTRACT}
* {MULTIPLY}
/ {DIVIDE}
后续篇章会继续完成后续的控件操作,请稍等哈