python pyautogui 鍵盤鼠標(biāo)自動(dòng)化

1、安裝模塊:
在Windows 上,不需要安裝其他模塊。
? 在OS X 上,運(yùn)行sudo pip3 install pyobjc-framework-Quartz,sudo pip3 install
pyobjc-core,然后sudo pip3 install pyobjc。
? 在Linux 上,運(yùn)行sudo pip3 install python3-xlib,sudo apt-get install scrot,sudo
apt-get install python3-tk,以及sudo apt-get install python3-dev(Scrot 是
PyAutoGUI 使用的屏幕快照程序)。
在這些依賴安裝后,運(yùn)行pip install pyautogu(i 或在OS X和Linux上運(yùn)行pip3),
安裝pyautogui。
2、pyautogui執(zhí)行時(shí),如果鼠標(biāo)移到屏幕左上角,將導(dǎo)致pyautogui產(chǎn)生pyautogui.FailSafeException異常。如果設(shè)置FAILSAEF=False將禁止這項(xiàng)功能。

import pyautogui
pyautogui.PAUSE = 1
pyautogui.FAILSAFE = True
這里我們導(dǎo)入pyautogui,并將pyautogui.PAUSE 設(shè)置為1,即每次函數(shù)調(diào)用后
暫停一秒。將pyautogui.FAILSAFE 設(shè)置為True,啟動(dòng)自動(dòng)防故障功能。

獲取屏幕分辨率

import pyautogui
pyautogui.size()
(1920, 1080)
width, height = pyautogui.size()
移動(dòng)鼠標(biāo),moveTo,duration指定移動(dòng)的時(shí)間
import pyautogui
for i in range(10):
pyautogui.moveTo(100, 100, duration=0.25)
pyautogui.moveTo(200, 100, duration=0.25)
pyautogui.moveTo(200, 200, duration=0.25)
pyautogui.moveTo(100, 200, duration=0.25)
從當(dāng)前位置移動(dòng)鼠標(biāo)moveRel
import pyautogui
for i in range(10):
pyautogui.moveRel(100, 0, duration=0.25)
pyautogui.moveRel(0, 100, duration=0.25)
pyautogui.moveRel(-100, 0, duration=0.25)
pyautogui.moveRel(0, -100, duration=0.25)
獲取鼠標(biāo)位置:pyautogui.position()
3、實(shí)時(shí)顯示鼠標(biāo)當(dāng)前位置:
import pyautogui
print('Press Ctrl-C to quit.')
try:
while True:
# Get and print the mouse coordinates.
x, y = pyautogui.position()
positionStr = 'X: ' + str(x).rjust(4) + ' Y: ' + str(y).rjust(4)
print(positionStr, end='')
print('\b' * len(positionStr), end='', flush=True)

except KeyboardInterrupt:
print('\nDone.')

點(diǎn)擊鼠標(biāo)左鍵:pyautogui.click(100,150,button='left')
pyautogui.click(200,250,button='right')
pyautogui. mouseDown(),pyautogui.mouseUp(),
pyautogui.doubleClick()雙擊鼠標(biāo)左鍵
pyautogui.rightClick() 和pyautogui.middleClick() 右鍵和中建雙擊

拖動(dòng)鼠標(biāo):
import pyautogui, time
? time.sleep(5)
? pyautogui.click() # click to put drawing program in focus
distance = 200
while distance > 0:
? pyautogui.dragRel(distance, 0, duration=0.2) # move right
? distance = distance - 5
? pyautogui.dragRel(0, distance, duration=0.2) # move down
? pyautogui.dragRel(-distance, 0, duration=0.2) # move left
distance = distance - 5
pyautogui.dragRel(0, -distance, duration=0.2) # move up

pyautogui.scroll(200) 滾動(dòng)鼠標(biāo)
屏幕快照:

import pyautogui
im = pyautogui.screenshot()
im.getpixel((0, 0))
(176, 176, 175)
im.getpixel((50, 200))
(130, 135, 144)

像素比較:pyautogui.pixelMatchesColor(50, 200, (130, 135, 144))
實(shí)時(shí)顯示鼠標(biāo)位置和像素:
import pyautogui
print('Press Ctrl-C to quit.')
try:
while True:
# Get and print the mouse coordinates.
x, y = pyautogui.position()
positionStr = 'X: ' + str(x).rjust(4) + ' Y: ' + str(y).rjust(4)
pixelColor = pyautogui.screenshot().getpixel((x, y))
positionStr += ' RGB: (' + str(pixelColor[0]).rjust(3)
positionStr += ', ' + str(pixelColor[1]).rjust(3)
positionStr += ', ' + str(pixelColor[2]).rjust(3) + ')'
print(positionStr, end='')
print('\b' * len(positionStr), end='', flush=True)

except KeyboardInterrupt:
print('\nDone.')

獲取圖像位置:locateOnScreen

import pyautogui
pyautogui.locateOnScreen('submit.png')
(643, 745, 70, 29)

list(pyautogui.locateAllOnScreen('submit.png'))
[(643, 745, 70, 29), (1007, 801, 70, 29)]
獲取中心坐標(biāo):center
pyautogui.locateOnScreen('submit.png')
(643, 745, 70, 29)
pyautogui.center((643, 745, 70, 29))
(678, 759)
pyautogui.click((678, 759))

打字:typewrite
pyautogui.click(100, 100); pyautogui.typewrite('Hello world!')

pyautogui.typewrite(['a', 'b', 'left', 'left', 'X', 'Y'])

typewrite鍵列表
'a', 'b', 'c', 'A', 'B', 'C', '1', '2', '3',
'!', '@', '#',等等
單個(gè)字符的鍵
'enter'(or 'return' or '\n') 回車鍵
'esc' Esc 鍵
'shiftleft', 'shiftright' 左右Shift 鍵
'altleft', 'altright' 左右Alt 鍵
'ctrlleft', 'ctrlright' 左右Ctrl 鍵
'tab'(or '\t') Tab 鍵
'backspace', 'delete' Backspace 和Delete 鍵
'pageup', 'pagedown' Page Up 和Page Down 鍵
'home', 'end' Home 和End 鍵
'up', 'down', 'left', 'right' 上下左右箭頭鍵
'f1', 'f2', 'f3',等等 F1 至F12 鍵
'volumemute', 'volumedown', 'volumeup'
靜音、減小音量、放大音量鍵(有些鍵盤沒有這些鍵,但你
的操作系統(tǒng)仍能理解這些模擬的按鍵)
'pause' Pause 鍵
'capslock', 'numlock', 'scrolllock' Caps Lock,Num Lock 和Scroll Lock 鍵
'insert' Ins 或Insert 鍵
'printscreen' Prtsc 或Print Screen 鍵
'winleft', 'winright' 左右Win 鍵(在Windows 上)
'command' Command 鍵(在OS X 上)
'option' Option 鍵(在OS X 上)

按下鍵
pyautogui.keyDown() 和pyautogui.keyUp()

組合鍵
pyautogui.keyDown('shift'); pyautogui.press('4'); pyautogui.keyUp('shift')
熱鍵組合:Hotkey
pyautogui.hotkey('ctrl', 'c')

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容