1 导入模块
import pyautogui
2 前置参数
1 自动 防故障功能
:::info FAILSAFE=True 时,鼠标移到屏幕左上角即可中断程序 :::
pyautogui.FAILSAFE = True
2 停顿功能
pyautogui.PAUSE = 1
3 获取屏幕分辨率
width, height = pyautogui.size()
print(width, height)
4 鼠标操作
1 鼠标位置
p = pyautogui.position()
print(p)
2 鼠标移动
1 绝对位置
:::info duration: 移动时间 :::
pyautogui.moveTo(100, 100, duration=0.5)
2 相对位置
:::info relative: 相对 :::
pyautogui.moveRel(0, 100, duration=0.5)
3 鼠标移动高级设置
1 开始很慢,不断加速
pyautogui.moveTo(100, 100, 2, pyautogui.easeInQuad)
2 开始很快,不断减速
pyautogui.moveTo(100, 100, 2, pyautogui.easeOutQuad)
3 开始和结束都快,中间比较慢
pyautogui.moveTo(100, 100, 2, pyautogui.easeInOutQuad)
4 一步一徘徊前进
pyautogui.moveTo(100, 100, 2, pyautogui.easeInBounce)
5 徘徊幅度更大,甚至超过起点和终点
pyautogui.moveTo(100, 100, 2, pyautogui.easeInElastic)
3 鼠标点击
pyautogui.click()
pyautogui.click(100, 100)
pyautogui.click(100, 200, button='left')
pyautogui.click(100, 200, button='right')
pyautogui.click(100, 200, button='middle')
4 鼠标双击
pyautogui.doubleClick()
5 鼠标右键
pyautogui.rightClick()
6 鼠标中键
pyautogui.middleClick()
7 鼠标长按
pyautogui.mouseDown()
8 鼠标释放
pyautogui.mouseUp()
9 鼠标拖动
1 绝对位置
pyautogui.dragTo(100, 100, duration=0.5)
2 相对位置
pyautogui.dragRel(100, 100, duration=0.5)
10 鼠标滚动
:::info 正数向上,负数向下 :::
pyautogui.scroll(-300)
5 键盘操作
1 按下某键
:::info 大写字母会自动执行 shift :::
pyautogui.press('V')
2 长按某键
pyautogui.keyDown('ctrl')
pyautogui.keyDown('v')
3 释放某键
pyautogui.keyUp('ctrl')
pyautogui.keyUp('v')
4 组合键
pyautogui.hotkey('ctrl','v')
5 输入内容
:::info
interval:间隔时间
Enter:执行回车
:::
pyautogui.typewrite('python',interval=1)
pyautogui.typewrite(['p', 'y', 't', 'h', 'o', 'n', 'Enter'], interval=0.5)
6 键盘特殊按键
键盘字符串 | 说明 |
---|---|
enter(或return 或 \n) | 回车 |
esc | ESC键 |
shiftleft, shiftright | 左右SHIFT键 |
altleft, altright | 左右ALT键 |
ctrlleft, ctrlright | 左右CTRL键 |
tab (\t) | TAB键 |
backspace, delete | BACKSPACE 、DELETE键 |
pageup, pagedown | PAGE UP 和 PAGE DOWN键 |
home, end | HOME 和 END键 |
up, down, left,right | 箭头键 |
f1, f2, f3…. f12 | F1…….F12键 |
volumemute, volumedown,volumeup | 声音变大变小静音(有些键盘没有) |
pause | PAUSE键,暂停键 |
capslock | CAPS LOCK 键 |
numlock | NUM LOCK 键 |
scrolllock | SCROLLLOCK 键 |
insert | INSERT键 |
printscreen | PRINT SCREEN键 |
winleft, winright | Win键(windows ) |
command | command键(Mac OS X ) |
option | option(Mac OS X) |
6 图片操作
1 屏幕截图
:::info 需要更新 pillow 模块 :::
img = pyautogui.screenshot()
img = pyautogui.screenshot(region=(0,0,100,100))
img.save('screenshot.png')
2 获取像素RGB元祖
img = pyautogui.screenshot()
rgb = img.getpixel((500,500))
print(rgb)
3 像素匹配
match = pyautogui.pixelMatchesColor(500,500,rgb)
print(match)
4 图像识别
1 取图片在屏幕相应位置的初始坐标与宽度高度
location = pyautogui.locateOnScreen('test.png')
location2 = pyautogui.locateOnScreen('test.png', region=(0, 0, 1920, 1080))
locations = pyautogui.locateAllOnScreen('test.png')
print(location)
print(location2)
print(list(locations))
2 取图片在屏幕相应位置的中心点
centerLocation = pyautogui.locateCenterOnScreen('test.png')
print(centerLocation)
3 图片匹配
:::info
返回找到 needleImage 在 haystackImage 里面的坐标,如果没找到返回None
grayscale: 灰度值匹配,去色对比
:::
location = pyautogui.locate(needleImage='wx.png', haystackImage='wx3.png', grayscale=True)
print(location)
5 获取 中心点
center = pyautogui.center(location)
print(center)
6 坐标是否在屏幕
onscreen=pyautogui.onScreen(100,-1)
print(onscreen)
7 信息框
1 确认框
confirm = pyautogui.confirm(text='是否继续?',title='确认框',buttons=('是','否'))
print(confirm)
2 警示框
alert = pyautogui.alert(text='测试成功',title='警示框',button='确定')
print(alert)
3 输入框
prompt = pyautogui.prompt(text='请输入账号:',title='登录账号')
print(prompt)
4 密码框
password = pyautogui.password(text='请输入密码:',title='登录密码',mask='*')
print(password)