1. 用GUI 自動化控制鍵盤和鼠標第18章 (代碼下載)
pyautogui模塊可以向Windows、OS X 和Linux 發送虛擬按鍵和鼠標點擊。根據使用的操作系統,在安裝pyautogui之前,可能需要安裝一些其他模塊。
- Windows: 不需要安裝其他模塊。
- OS X:
sudo pip3 install pyobjc-framework-Quartz
sudo pip3 install pyobjc-core
sudo pip3 install pyobjc
- Linux:
sudo pip3 install python3-xlib
sudo apt-get install scrot
sudo apt-get install python3-tk
sudo apt-get install python3-dev
依賴項安裝後安裝pyautogui:
pip install pyautogui
使用pyautogui需要注意防止或恢復GUI。通過ctrl+cg關閉程序或者。設定暫停和自動防故障,如下:
# 在每次函數調用後等一會兒,將pyautogui.PAUSE 變量設置爲要暫停的秒數
pyautogui.PAUSE = 1
# 設定自動防故障
pyautogui.FAILSAFE = True
常用函數如下:
函數 | 用途 |
---|---|
moveTo(x,y,duration) | 將鼠標移動到指定的x、y 座標,duration每次移動耗時多少秒,如果沒有數調用duration,鼠標就會馬上從一個點移到另一個點 |
moveRel(xOffset,yOffset,duration) | 相對於當前位置移動鼠標 |
pyautogui.position() | 確定鼠標當前位置 |
dragTo(x,y,duration) | 按下左鍵移動鼠標 |
dragRel(xOffset,yOffset,duration) | 按下左鍵,相對於當前位置移動鼠標 |
click(x,y,button) | 模擬點擊(默認是左鍵) |
rightClick() | 模擬右鍵點擊 |
middleClick() | 模擬中鍵點擊 |
doubleClick() | 模擬左鍵雙擊 |
pyautogui.scroll(units) | 模擬滾動滾輪。正參數表示向上滾動,負參數表示向下滾動 |
pyautogui.screenshot() | 返回屏幕快照的Image 對象 |
pyautogui.pixelMatchesColor(x,y,tuple) | 判斷x、y 座標處的像素與指定的顏色是否匹配。第一和第二個參數是xy整數座標。第三個參數是包含3個整數元組,表示RGB 顏色 |
pyautogui.locateOnScreen(image) | 將返回圖像iamge所在處當前屏幕圖像的座標,如果無法匹配返回None |
pyautogui.center(x,y,w,h) | 返回區域的中心座標 |
typewrite(message) | 鍵入給定消息字符串中的字符 |
typewrite([key1,key2,key3]) | 鍵入給定鍵字符串,pyautogui.KEYBOARD_KEYS查看鍵字符串的列表 |
press(key) | 按下並釋放給定鍵 |
keyDown(key) | 模擬按下給定鍵 |
keyUp(key) | 模擬釋放給定鍵 |
mouseDown(x,y,button) | 模擬在x、y 處按下指定鼠標按鍵 |
mouseUp(x,y,button) | 模擬在x、y 處釋放指定鍵 |
hotkey([key1,key2,key3]) | 模擬按順序按下給定鍵字符串,然後以相反的順序釋放 |
2 項目練習
2.1 項目:“現在鼠標在哪裏?”
在移動鼠標時隨時顯示 x y 座標和改點RGB值
# 顯示鼠標實時位置
import pyautogui
# 程序終止crtl+c
print('Press Ctrl-C to quit.')
try:
while True:
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) + ')'
# end結尾添加的字符,默認換行符
print(positionStr, end='')
# https://blog.csdn.net/lucosax/article/details/34963593
# 得到了許多\b 字符構成的字符串,長度與positionStr中保存的字符串長度一樣,效果就是擦除了前面打印的字符串\
# flush把文件從內存buffer(緩衝區)中強制刷新到硬盤中,同時清空緩衝區。實現動態效果
print('\b' * len(positionStr), end='', flush=True)
# 退出程序
except KeyboardInterrupt:
print('\nDone.')
2.2 項目:自動填表程序
實現自動填表,表格網站,地址http://autbor.com/form
import pyautogui, time
# Set these to the correct coordinates for your computer
# name輸入位置
nameField = (622, 199)
submitButton = (601, 978)
submitButtonColor = (31, 115, 230)
submitAnotherLink = (760, 224)
# 表單數據
formData = [{'name': 'Alice', 'fear': 'eavesdroppers', 'source': 'wand','robocop': 4, 'comments': 'Tell Bob I said hi.'},
{'name': 'Bob', 'fear': 'bees', 'source': 'amulet', 'robocop': 4,'comments': 'Please take the puppets out of the break room.'},
{'name': 'Carol', 'fear': 'puppets', 'source': 'crystal ball','robocop': 1, 'comments': 'Please take the puppets out of the break room.'},
{'name': 'Alex Murphy', 'fear': 'ED-209', 'source': 'money','robocop': 5, 'comments': 'Protect the innocent. Serve the publictrust. Uphold the law.'},]
# 暫停0.5s
pyautogui.PAUSE = 0.5
for person in formData:
# Give the user a chance to kill the script.
print('>>> 5 SECOND PAUSE TO LET USER PRESS CTRL-C <<<')
time.sleep(5)
# Wait until the form page has loaded.
# 判斷點submitButton和submitButtonColor的顏色是否匹配
while not pyautogui.pixelMatchesColor(submitButton[0], submitButton[1],submitButtonColor):
time.sleep(0.5)
print('Entering %s info...' % (person['name']))
# 從名字開始填寫
pyautogui.click(nameField[0], nameField[1])
# Fill out the Name field.
pyautogui.typewrite(person['name'] + '\t')
# Fill out the Greatest Fear(s) field.
pyautogui.typewrite(person['fear'] + '\t')
# Fill out the Source of Wizard Powers field.
if person['source'] == 'wand':
# 按下鍵
pyautogui.typewrite(['down'])
# 回車
pyautogui.typewrite(['enter'])
# tab
pyautogui.typewrite(['\t'])
elif person['source'] == 'amulet':
pyautogui.typewrite(['down', 'down'])
pyautogui.typewrite(['enter'])
pyautogui.typewrite(['\t'])
elif person['source'] == 'crystal ball':
pyautogui.typewrite(['down', 'down', 'down'])
pyautogui.typewrite(['enter'])
pyautogui.typewrite(['\t'])
elif person['source'] == 'money':
pyautogui.typewrite(['down', 'down', 'down', 'down'])
pyautogui.typewrite(['enter'])
pyautogui.typewrite(['\t'])
# Fill out the RoboCop field.
if person['robocop'] == 1:
pyautogui.typewrite([' ', '\t'])
elif person['robocop'] == 2:
pyautogui.typewrite(['right', '\t'])
elif person['robocop'] == 3:
pyautogui.typewrite(['right', 'right', '\t'])
elif person['robocop'] == 4:
pyautogui.typewrite(['right', 'right', 'right', '\t'])
elif person['robocop'] == 5:
pyautogui.typewrite(['right', 'right', 'right', 'right', '\t'])
# Fill out the Additional Comments field.
# 輸入文字
pyautogui.typewrite(person['comments'] + '\t')
# Click Submit.
#pyautogui.press('enter')
# Wait until form page has loaded.
print('Clicked Submit.')
time.sleep(5)
# Click the Submit another response link.
# 確認提交
pyautogui.click(submitAnotherLink[0], submitAnotherLink[1])