如下是pywinauto使用指南。這個窗口句柄能夠在Spy++中查看 (Microsoft Spy++(查看窗口句柄) 10.00.30319 官方最新綠色版)html
python自動化工具之pywinauto(一)python
python自動化工具之pywinauto(三)windows
//導入模塊 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鍵
======app
如下是記事本操做實例。工具
1 #! /usr/bin/env python 2 3 4 ''' 5 FuncName: johnny-pywinauto.py 6 Desc: study pywinauto 7 Date: 2016-10-10 14:30 8 Author: johnny 9 Home:http://blog.csdn.net/z_johnny 10 ''' 11 12 from pywinauto import application 13 from pykeyboard import PyKeyboard 14 import time 15 16 class Pywin(object): 17 """ 18 pywin framwork main class 19 tool_name : 程序名稱,支持帶路徑 20 windows_name : 窗口名字 21 """ 22 SLEEP_TIME = 1 23 24 def __init__(self): 25 """ 26 初始化方法,初始化一個app 27 """ 28 self.app = application.Application() 29 30 def run(self, tool_name): 31 """ 32 啓動應用程序 33 """ 34 35 self.app.start(tool_name) 36 time.sleep(1) 37 38 def connect(self, window_name): 39 """ 40 鏈接應用程序 41 app.connect_(path = r"c:\windows\system32\notepad.exe") 42 app.connect_(process = 2341) 43 app.connect_(handle = 0x010f0c) 44 """ 45 self.app.connect(title = window_name) 46 time.sleep(1) 47 48 def close(self, window_name): 49 """ 50 關閉應用程序 51 """ 52 self.app[window_name].Close() 53 time.sleep(1) 54 55 def max_window(self, window_name): 56 """ 57 最大化窗口 58 """ 59 self.app[window_name].Maximize() 60 time.sleep(1) 61 62 def menu_click(self, window_name, menulist): 63 """ 64 菜單點擊 65 """ 66 self.app[window_name].MenuSelect(menulist) 67 time.sleep(1) 68 69 def input(self, window_name, controller, content): 70 """ 71 輸入內容 72 """ 73 self.app[window_name][controller].TypeKeys(content) 74 time.sleep(1) 75 76 def click(self, window_name, controller): 77 """ 78 鼠標左鍵點擊 79 example: 80 下面兩個功能相同,下面支持正則表達式 81 app[u'關於「記事本」'][u'肯定'].Click() 82 app.window_(title_re = u'關於「記事本」').window_(title_re = u'肯定').Click() 83 """ 84 self.app[window_name][controller].Click() 85 time.sleep(1) 86 87 def double_click(self, window_name, controller, x = 0,y = 0): 88 """ 89 鼠標左鍵點擊(雙擊) 90 """ 91 self.app[window_name][controller].DoubleClick(button = "left", pressed = "", coords = (x, y)) 92 time.sleep(1) 93 94 def right_click(self, window_name, controller, order): 95 """ 96 鼠標右鍵點擊,下移進行菜單選擇 97 window_name : 窗口名 98 controller:區域名 99 order : 數字,第幾個命令 100 """ 101 self.app[window_name][controller].RightClick() 102 k = PyKeyboard() 103 for down in range(order): 104 k.press_key(k.down_key) 105 time.sleep(0.5) 106 k.press_key(k.enter_key) 107 time.sleep(1) 108 109 if __name__ == "__main__": 110 app = Pywin() 111 # 記事本例子 112 tool_name = "notepad.exe" 113 # 經過Spy++ 獲取window_name,即標題文本 114 window_name = u"無標題 - 記事本" 115 menulist = u"幫助->關於記事本" 116 # 經過Spy++ 獲取controller,即窗口類名 117 controller = "Edit" 118 content = u"johnny" 119 window_name_new = content + ".txt" 120 # 啓動程序,記事本只能開一個 121 app.run(tool_name) 122 123 app.connect(window_name) 124 125 # app.max_window(window_name) 126 app.menu_click(window_name,menulist) 127 app.click(u'關於記事本', u'肯定') 128 app.input(window_name,controller,content) 129 # Ctrl + a 全選 130 app.input(window_name,controller,"^a") 131 # 選擇複製 132 app.right_click(window_name,controller,3) 133 #選擇粘貼 134 app.right_click(window_name,controller,4) 135 k=PyKeyboard() 136 k.press_key(k.enter_key) 137 # Ctrl + v 粘貼 138 app.input(window_name,controller,"^v") 139 # Ctrl + s 保存 140 app.input(window_name,controller,"^s") 141 # 輸入文件名 142 app.input(u"另存爲",controller,content) 143 # 保存 144 app.click(u"另存爲","Button") 145 try: 146 app.click(u"確認另存爲","Button") 147 except: 148 pass 149 finally: 150 app.close(window_name_new)