以下是pywinauto使用指南。这个窗口句柄可以在Spy++中查看 (Microsoft Spy++(查看窗口句柄) 10.00.30319 官方最新绿色版)
二、pyuserinput使用方法
//导入模块 import pymouse,pykeyboard,os,sys
from pymouse import *
from pykeyboard import PyKeyboard
//分别定义一个实例
m = PyMouse()
k = PyKeyboard() 鼠标操作:
m.click(x,y,button,n) –鼠标点击
x,y –是坐标位置
buttong –1表示左键,2表示点击右键
n –点击次数,默认是1次,2表示双击 m.move(x,y) –鼠标移动到坐标(x,y) x_dim, y_dim = m.screen_size() –获得屏幕尺寸 键盘操作: k.type_string(‘Hello, World!’) –模拟键盘输入字符串
k.press_key(‘H’) –模拟键盘按H键
k.release_key(‘H’) –模拟键盘松开H键
k.tap_key(“H”) –模拟点击H键
k.tap_key(‘H’,n=2,interval=5) –模拟点击H键,2次,每次间隔5秒
k.tap_key(k.function_keys[5]) –点击功能键F5
k.tap_key(k.numpad_keys[5],3) –点击小键盘5,3次 联合按键模拟
例如同时按alt+tab键盘
k.press_key(k.alt_key) –按住alt键
k.tap_key(k.tab_key) –点击tab键
k.release_key(k.alt_key) –松开alt键
======
以下是记事本操作实例。
#! /usr/bin/env python '''
FuncName: johnny-pywinauto.py
Desc: study pywinauto
Date: 2016-10-10 14:30
Author: johnny
Home:http://blog.****.net/z_johnny
''' from pywinauto import application
from pykeyboard import PyKeyboard
import time class Pywin(object):
"""
pywin framwork main class
tool_name : 程序名称,支持带路径
windows_name : 窗口名字
"""
SLEEP_TIME = 1 def __init__(self):
"""
初始化方法,初始化一个app
"""
self.app = application.Application() def run(self, tool_name):
"""
启动应用程序
""" self.app.start(tool_name)
time.sleep(1) def connect(self, window_name):
"""
连接应用程序
app.connect_(path = r"c:\windows\system32\notepad.exe")
app.connect_(process = 2341)
app.connect_(handle = 0x010f0c)
"""
self.app.connect(title = window_name)
time.sleep(1) def close(self, window_name):
"""
关闭应用程序
"""
self.app[window_name].Close()
time.sleep(1) def max_window(self, window_name):
"""
最大化窗口
"""
self.app[window_name].Maximize()
time.sleep(1) def menu_click(self, window_name, menulist):
"""
菜单点击
"""
self.app[window_name].MenuSelect(menulist)
time.sleep(1) def input(self, window_name, controller, content):
"""
输入内容
"""
self.app[window_name][controller].TypeKeys(content)
time.sleep(1) def click(self, window_name, controller):
"""
鼠标左键点击
example:
下面两个功能相同,下面支持正则表达式
app[u'关于“记事本”'][u'确定'].Click()
app.window_(title_re = u'关于“记事本”').window_(title_re = u'确定').Click()
"""
self.app[window_name][controller].Click()
time.sleep(1) def double_click(self, window_name, controller, x = 0,y = 0):
"""
鼠标左键点击(双击)
"""
self.app[window_name][controller].DoubleClick(button = "left", pressed = "", coords = (x, y))
time.sleep(1) def right_click(self, window_name, controller, order):
"""
鼠标右键点击,下移进行菜单选择
window_name : 窗口名
controller:区域名
order : 数字,第几个命令
"""
self.app[window_name][controller].RightClick()
k = PyKeyboard()
for down in range(order):
k.press_key(k.down_key)
time.sleep(0.5)
k.press_key(k.enter_key)
time.sleep(1) if __name__ == "__main__":
app = Pywin()
# 记事本例子
tool_name = "notepad.exe"
# 通过Spy++ 获取window_name,即标题文本
window_name = u"无标题 - 记事本"
menulist = u"帮助->关于记事本"
# 通过Spy++ 获取controller,即窗口类名
controller = "Edit"
content = u"johnny"
window_name_new = content + ".txt"
# 启动程序,记事本只能开一个
app.run(tool_name) app.connect(window_name) # app.max_window(window_name)
app.menu_click(window_name,menulist)
app.click(u'关于记事本', u'确定')
app.input(window_name,controller,content)
# Ctrl + a 全选
app.input(window_name,controller,"^a")
# 选择复制
app.right_click(window_name,controller,3)
#选择粘贴
app.right_click(window_name,controller,4)
k=PyKeyboard()
k.press_key(k.enter_key)
# Ctrl + v 粘贴
app.input(window_name,controller,"^v")
# Ctrl + s 保存
app.input(window_name,controller,"^s")
# 输入文件名
app.input(u"另存为",controller,content)
# 保存
app.click(u"另存为","Button")
try:
app.click(u"确认另存为","Button")
except:
pass
finally:
app.close(window_name_new)