1.安裝 pywinauto app
pip install pywinautoide
2.打開PC的.exe可執行文件的,一種方式就夠用了spa
from pywinauto import application app = application.Application().start('notepad.exe')
效果:3d
3.能夠看到窗口的名字是 '無標題-記事本' code
打印一下這個窗口輸入的位置blog
print(app[chuangkou].print_control_identifiers())
結果ip
Control Identifiers: Notepad - '無標題 - 記事本' (L473, T291, R1267, B688) ['Notepad', '無標題 - 記事本', '無標題 - 記事本Notepad'] child_window(title="無標題 - 記事本", class_name="Notepad") | | Edit - '' (L481, T341, R1259, B658) | ['Edit', '無標題 - 記事本Edit'] | child_window(class_name="Edit") | | StatusBar - '' (L481, T658, R1259, B680) | ['StatusBar 第 1 行,第 1 列', 'StatusBar', '無標題 - 記事本StatusBar'] | child_window(class_name="msctls_statusbar32")
4.寫一個試試:cmd
from pywinauto import application app = application.Application().start('notepad.exe') chuangkou = '無標題-記事本' #print(app[chuangkou].print_control_identifiers()) app[chuangkou].Edit.type_keys("個人第一個輸入 哈哈\n",with_spaces=True,with_newlines=True)#with_spaces空格不減 app[chuangkou].Edit.type_keys("可喜可賀 哈哈\n",with_spaces=True,with_newlines=True)#with_newlines換行
結果:it
5.點擊一下菜單欄試試-->效果: 點擊文件--退出--不保存pip
第一步:
from pywinauto import application app = application.Application().start('notepad.exe') chuangkou = '無標題-記事本' #print(app[chuangkou].print_control_identifiers()) app[chuangkou].Edit.type_keys("個人第一個輸入 哈哈\n",with_spaces=True,with_newlines=True)#with_spaces空格不減 app[chuangkou].Edit.type_keys("可喜可賀 哈哈\n",with_spaces=True,with_newlines=True)#with_newlines換行 app[chuangkou].menu_select("文件->退出")
6.效果
7.切換到彈出窗口點擊不保存按鈕
from pywinauto import application app = application.Application().start('notepad.exe') chuangkou = '無標題-記事本' #print(app[chuangkou].print_control_identifiers()) app[chuangkou].Edit.type_keys("個人第一個輸入 哈哈\n",with_spaces=True,with_newlines=True)#with_spaces空格不減 app[chuangkou].Edit.type_keys("可喜可賀 哈哈\n",with_spaces=True,with_newlines=True)#with_newlines換行 app[chuangkou].menu_select("文件->退出") out_1 = '記事本' anniu = '不保存' app[out_1][anniu].click()
效果:
8.鼠標操做:
from time import sleep from pywinauto.mouse import move x=y=20 for i in range(10): xq=x*i yq=y*i sleep(1) move(coords=(xq,yq))#移動鼠標
9鍵盤操做:1.
#引入鍵盤操做 from pywinauto.keyboard import send_keys #發送鍵盤指令 send_keys('11111111')
2.
#引入鍵盤操做 from time import sleep from pywinauto.keyboard import send_keys #發送鍵盤指令 send_keys('{VK_LWIN}cmd{VK_RETURN}') sleep(0.5) send_keys('ipconfig{VK_RETURN}')
效果1:鼠標的光標位置寫了值
效果2:
經常使用ASCII碼/ASCII[ASCII]
ESC鍵:VK_ESCAPE (27)
回車鍵:VK_RETURN (13)
TAB鍵:VK_TAB (9)
Caps Lock鍵:VK_CAPITAL (20)
Shift鍵:VK_SHIFT (16)
Ctrl鍵:VK_CONTROL (17)
Alt鍵:VK_MENU (18)
空格鍵:VK_SPACE (32)
退格鍵:VK_BACK (8)
左徽標鍵:VK_LWIN (91)
右徽標鍵:VK_RWIN (92)
鼠標右鍵快捷鍵:VK_APPS (93)
Insert鍵:VK_INSERT (45)
Home鍵:VK_HOME (36)
Page Up:VK_PRIOR (33)
PageDown:VK_NEXT (34)
End鍵:VK_END (35)
Delete鍵:VK_DELETE (46)
方向鍵(←):VK_LEFT (37)
方向鍵(↑):VK_UP (38)
方向鍵(→):VK_RIGHT (39)
方向鍵(↓):VK_DOWN (40)
F1鍵:VK_F1 (112)
F2鍵:VK_F2 (113)
F3鍵:VK_F3 (114)
F4鍵:VK_F4 (115)
F5鍵:VK_F5 (116)
F6鍵:VK_F6 (117)
F7鍵:VK_F7 (118)
F8鍵:VK_F8 (119)
F9鍵:VK_F9 (120)
F10鍵:VK_F10 (121)
F11鍵:VK_F11 (122)
F12鍵:VK_F12 (123)
Num Lock鍵:VK_NUMLOCK (144)
小鍵盤0:VK_NUMPAD0 (96)
小鍵盤1:VK_NUMPAD1 (97)
小鍵盤2:VK_NUMPAD2 (98)
小鍵盤3:VK_NUMPAD3 (99)
小鍵盤4:VK_NUMPAD4 (100)
小鍵盤5:VK_NUMPAD5 (101)
小鍵盤6:VK_NUMPAD6 (102)
小鍵盤7:VK_NUMPAD7 (103)
小鍵盤8:VK_NUMPAD8 (104)
小鍵盤9:VK_NUMPAD9 (105)
小鍵盤。:VK_DECIMAL (110)
小鍵盤*:VK_MULTIPLY (106)
小鍵盤+:VK_ADD (107)
小鍵盤-:VK_SUBTRACT (109)
小鍵盤/:VK_DIVIDE (111)
Pause Break鍵:VK_PAUSE (19)
Scroll Lock鍵:VK_SCROLL (145)
10.經常使用的Shift 和Ctrl 和Alt 組合按鍵
"+" ----------------------------> 按Shift
"^" ---------------------------->按Ctrl
"%" ----------------------------> 按Alt
"^s" ----------------------------> 按Ctrl+S進行保存的操做
試試:
#引入鍵盤操做 from pywinauto.keyboard import send_keys #發送鍵盤指令 send_keys("+a")
效果: