简介

机器人流程自动化(Robotic Process Automation, RPA)是流程自动话技术,可以替代手工操作软件,完成大量重复性工作。PyAutoGUI是一款跨平台/无依赖的自动化测试工具,目测只能控制鼠标/键盘/获取屏幕尺寸/弹出消息框/截屏。

安装

pip install pyautogui

鼠标操作

获取屏幕分辨率大小和鼠标指针的当前坐标

import pyautogui

# size()函数:获取屏幕分辨率大小
scr_size = pyautogui.size()
print(scr_size)
w, h = scr_size.width, scr_size.height
w, h = scr_size[0], scr_size[1]
w, h = pyautogui.size()
print(w, h)

# position()函数:返回鼠标指针的当前坐标
mouse_pos = pyautogui.position()
print(mouse_pos)
x, y = mouse_pos.x, mouse_pos.y
x, y = mouse_pos[0], mouse_pos[1]
x, y = pyautogui.position()
print(x, y)

移动鼠标

import pyautogui
pyautogui.moveTo([1392, 247], duration=1)
pyautogui.moveRel([20, 30], duration=1)

点击鼠标

import pyautogui
pyautogui.click()
pyautogui.click(x=566, y=193)
pyautogui.mouseDown(button='right', x=100, y=200)
pyautogui.mouseUp(button='left', x=100, y=200)
pyautogui.doubleClick()  # 模拟双击鼠标左键
pyautogui.rightClick()  # 模拟单击鼠标右键
pyautogui.middleClick()  # 模拟单击鼠标滚轮

滚动鼠标

import pyautogui
pyautogui.scroll(10)
pyautogui.scroll(-10)
pyautogui.scroll(10, x=100, y=100)

拖动鼠标

import pyautogui
pyautogui.dragTo([1392, 47], duration=1)
pyautogui.dragRel([20, 30], duration=1)

键盘控制

import pyautogui
screenWidth, screenHeight = pyautogui.size()
currentMouseX, currentMouseY = pyautogui.position()
pyautogui.moveTo(100, 150)
pyautogui.click()
pyautogui.moveRel(None, 10)  # move mouse 10 pixels down
pyautogui.doubleClick()
pyautogui.moveTo(500, 500, duration=2, tween=pyautogui.tweens.easeInOutQuad)  # use tweening/easing function to move mouse over 2 seconds.
pyautogui.typewrite('Hello world!', interval=0.25)  # type with quarter-second pause in between each key
pyautogui.press('esc')
pyautogui.keyDown('shift')
pyautogui.typewrite(['left', 'left', 'left', 'left', 'left', 'left'])
pyautogui.keyUp('shift')
pyautogui.hotkey('ctrl', 'c')
# 空格
import pyautogui
pyautogui.press('space')
# 输入字符
pyautogui.typewrite('Hello')
pyautogui.typewrite('Hello', interval=0.5)
pyautogui.typewrite(['g', 'u', 'i', 'left', 'backspace'])
# 快捷键
pyautogui.hotkey('ctrl', 'a')

输入汉字

复制汉字

import pyperclip
import pyautogui
def paste(text):  
    pyperclip.copy(text)
    pyautogui.hotkey('ctrl', 'v')
a = '学而时习之'
pyautogui.click(442, 348)
paste(a)

显示消息框

消息框

import pyautogui
pyautogui.alert(text='发生错误!', title='警告', button='OK')

多按钮消息框

import pyautogui
pyautogui.confirm(text='是否保存更改?', title='保存', buttons=['OK', 'Cancel'])

输入文本消息框

import pyautogui
pyautogui.prompt(text='请输入打印份数', title='打印', default='1')

输入密码消息框

import pyautogui
pyautogui.password(text='请输入密码', title='登录', default='', mask='*')

图像匹配

import pyautogui
btn_pos = pyautogui.locateOnScreen('Win.png')
print(btn_pos)
print(btn_pos[1])
print(btn_pos.top)
center_pos = pyautogui.center(btn_pos)
print(center_pos)
print(center_pos[0])
print(center_pos.x)

locateOnScreen函数会从屏幕左上角开始,从左到右,从上到下,顺序遍历,直至匹配到图像为止,返回Box对象。
center函数会提取图像的中心点,返回Point对象。

import pyautogui
btn_pos = pyautogui.locateCenterOnScreen('Win.png')
print(btn_pos)
print(btn_pos[1])
print(btn_pos.y)

locateCenterOnScreen相当于locateOnScreen和center。

灰度匹配

import pyautogui
import time
# 不启用匹配灰度
start_time = time.time()
btn_pos = pyautogui.locateCenterOnScreen('Win.png')
end_time = time.time()
print(btn_pos, end_time - start_time)
# 启用匹配灰度
start_time = time.time()
btn_pos = pyautogui.locateCenterOnScreen('Win.png', grayscale=True)
end_time = time.time()
print(btn_pos, end_time - start_time)

降低匹配精度

import pyautogui
# 不降低匹配精度
btn_pos = pyautogui.locateCenterOnScreen('Win2.png')
print(btn_pos)
# 降低匹配精度
btn_pos = pyautogui.locateCenterOnScreen('Win2.png', confidence=0.9)
print(btn_pos)

限定区域

import pyautogui
import time
# 不限定区域
start_time = time.time()
btn_pos = pyautogui.locateCenterOnScreen('Win.png')
end_time = time.time()
print(btn_pos, end_time - start_time)
# 限定区域
start_time = time.time()
btn_pos = pyautogui.locateCenterOnScreen('Win.png', region=(0, 980, 100, 100))
end_time = time.time()
print(btn_pos, end_time - start_time)

获取像素点颜色

import pyautogui
pix = pyautogui.pixel(500, 500)
print(pix)  # RGB颜色元组
pix_check = pyautogui.pixelMatchesColor(500, 500, (171, 92, 33))
print(pix_check)
pix_check = pyautogui.pixelMatchesColor(550, 550, (171, 92, 33), tolerance=10)
print(pix_check)

pixelMatchesColor函数验证某一点是否为指定颜色,返回布尔类型。
pixelMatchesColor函数的tolerance可以模糊匹配颜色,并设置允许的偏差范围。

截屏

import pyautogui
im1 = pyautogui.screenshot()
im1.save('my_screenshot.png')
im2 = pyautogui.screenshot('my_screenshot2.png')
import pyautogui
im = pyautogui.screenshot()
print(im)
im = pyautogui.screenshot(region=(0, 0, 300, 400))
im = pyautogui.screenshot('pic.png')
im = pyautogui.screenshot('pic.png', region=(0, 0, 300, 400))
im = pyautogui.screenshot()
pix = im.getpixel((500, 500))
print(pix)

im为Pillow库的Image对象。

定位截屏

import pyautogui
button7location = pyautogui.locateOnScreen('button.png') # returns (left, top, width, height) of matching region
print(button7location)
# (1416, 562, 50, 41)
buttonx, buttony = pyautogui.center(button7location)
buttonx, buttony
# (1441, 582)
pyautogui.click(buttonx, buttony)  # clicks the center of where the button was found

案例:搜索

import pyautogui
import pyperclip
import time
def paste(text):
    pyperclip.copy(text)
    pyautogui.hotkey('ctrl', 'v')
    

# 启用自动防故障功能,左上角的坐标为(0,0),
# 将鼠标移到屏幕的左上角,来抛出failSafeException异常
pyautogui.FAILSAFE = True

# 调用在执行动作后暂停的秒数,
# 只能在执行一些pyautogui动作后才能使用,建议用time.sleep
pyautogui.PAUSE = 1
pyautogui.alert(text='请在单击“确定”按钮后,立即手动将\n浏览器窗口前置并最大化。', title='提示', button='OK')
time.sleep(3)
pyautogui.click(220, 50)
pyautogui.hotkey('ctrl', 'a')
paste('https://www.baidu.com')
pyautogui.press('enter')
pyautogui.click(650, 325)
paste('Python')
pyautogui.press('enter')

参考

http://pyautogui.readthedocs.io/en/latest/index.html https://github.com/asweigart/pyautogui
https://github.com/asweigart/sushigoroundbot