一、环境安装
1.命令行安装方法
pip install pywinauto==0.6.7
2.手动安装方法
安装包下载链接:
pyWin32: python调用windows api的库
https://sourceforge.net/projects/pywin32/files/pywin32/Build 220/
comtypes: COM接口的调度https://github.com/enthought/comtypes/releases
six: 用来兼容Python2和Python3的库
https://pypi.org/project/six/
Pillow:可选,用来做屏幕截图的
https://pypi.org/project/Pillow/2.7.0/
Pywinauto:PC端自动化工具
https://github.com/pywinauto/pywinauto/releases
解压缩后执行 python setup.py install
注:建议使用第一种命令行安装,方便。
3.环境检查
命令行中打开python解释器,运行以下代码,windows自带的记事本会被启动,若无报错,则证明pywinauto已安装成功。
1
2
|
from pywinauto.application import Application app = Application(backend = "uia" ).start( "notepad.exe" ) |
二、封装函数
# -*- coding: utf-8 -*- import time,os from pywinauto import application ''' 安装程序中,键盘快捷键对应码表如下: 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} ''' class Tool_Installer_Error(Exception): """Application has not been connected to a process yet""" pass # pragma: no cover class Tool_Installer(object): ''' 控件中有一个极其重要的方法wrapper_object() 如:object_func = dlg['Button1'].wrapper_object() print(dir(object_func)) #打印控件所有可用的方法 ''' def __init__(self): self.app_master = None self.app_window = None self.app_dlg = None def app_start(self,app_full_path,timeout=0.5): print('start application') if not os.path.exists(app_full_path): raise Tool_Installer_Error("%s does not exists" %(app_full_path)) self.app_master = application.Application().start(app_full_path) time.sleep(timeout) def app_connect(self,title_re,class_name,backend='win32',timeout=0.5): print('get application') self.app_window = application.Application(backend=backend).connect(title_re=title_re, class_name=class_name,timeout=timeout) def app_get_dlg(self,title): print('get dialog on application') self.app_dlg = self.app_window.window(title=title) #安装robot发现title_re参数失效 def app_get_all_widget(self): print('get all widget on dialog') widgets = self.app_dlg.print_control_identifiers() return widgets def app_wait_button_active(self,widget_name): print('wait for button active') while not self.app_dlg[widget_name].is_enabled(): time.sleep(3) def app_widget_click(self,widget_name,timeout=5): print('click button on %s' %(widget_name)) # 智能等待窗口部件出现,超时时间为5S self.app_dlg[widget_name].wait(wait_for="visible", timeout=timeout) self.app_dlg[widget_name].click() def app_widget_send_key(self,widget_name,key,timeout=5): print('send %s on %s' %(key,widget_name)) # 智能等待窗口部件出现,超时时间为5S self.app_dlg[widget_name].wait(wait_for="visible", timeout=timeout) self.app_dlg[widget_name].type_keys(key) def app_widget_get_text(self,widget_name): object_func = self.app_dlg[widget_name].wrapper_object() return object_func.window_text() if __name__ == '__main__': #安装robotframework robot_file_path = r'D:software obotframework-2.8.7.win32.exe' robot = Tool_Installer() app_master = robot.app_start(robot_file_path) robot.app_connect('Setup','#32770') robot.app_get_dlg('Setup') robot.app_widget_send_key(widget_name='Button2',key='%N') robot.app_widget_send_key(widget_name='Button3',key='%N') robot.app_widget_send_key(widget_name='Button2',key='%N') print(time.time()) print(robot.app_dlg.exists()) print(time.time()) robot.app_widget_send_key(widget_name='Button2',key='{ENTER}',timeout=60)