1 导入模块


  1. import pyautogui

2 前置参数


1 自动 防故障功能

:::info FAILSAFE=True 时,鼠标移到屏幕左上角即可中断程序 :::

  1. pyautogui.FAILSAFE = True

2 停顿功能

  1. pyautogui.PAUSE = 1

3 获取屏幕分辨率


  1. width, height = pyautogui.size()
  2. print(width, height)

4 鼠标操作


1 鼠标位置

  1. p = pyautogui.position()
  2. print(p)

2 鼠标移动

1 绝对位置

:::info duration: 移动时间 :::

  1. pyautogui.moveTo(100, 100, duration=0.5)

2 相对位置

:::info relative: 相对 :::

  1. pyautogui.moveRel(0, 100, duration=0.5)

3 鼠标移动高级设置

1 开始很慢,不断加速
  1. pyautogui.moveTo(100, 100, 2, pyautogui.easeInQuad)

2 开始很快,不断减速
  1. pyautogui.moveTo(100, 100, 2, pyautogui.easeOutQuad)

3 开始和结束都快,中间比较慢
  1. pyautogui.moveTo(100, 100, 2, pyautogui.easeInOutQuad)

4 一步一徘徊前进
  1. pyautogui.moveTo(100, 100, 2, pyautogui.easeInBounce)

5 徘徊幅度更大,甚至超过起点和终点
  1. pyautogui.moveTo(100, 100, 2, pyautogui.easeInElastic)

3 鼠标点击

  1. pyautogui.click()
  2. pyautogui.click(100, 100)
  3. pyautogui.click(100, 200, button='left')
  4. pyautogui.click(100, 200, button='right')
  5. pyautogui.click(100, 200, button='middle')

4 鼠标双击

  1. pyautogui.doubleClick()

5 鼠标右键

  1. pyautogui.rightClick()

6 鼠标中键

  1. pyautogui.middleClick()

7 鼠标长按

  1. pyautogui.mouseDown()

8 鼠标释放

  1. pyautogui.mouseUp()

9 鼠标拖动

1 绝对位置

  1. pyautogui.dragTo(100, 100, duration=0.5)

2 相对位置

  1. pyautogui.dragRel(100, 100, duration=0.5)

10 鼠标滚动

:::info 正数向上,负数向下 :::

  1. pyautogui.scroll(-300)

5 键盘操作


1 按下某键

:::info 大写字母会自动执行 shift :::

  1. pyautogui.press('V')

2 长按某键

  1. pyautogui.keyDown('ctrl')
  2. pyautogui.keyDown('v')

3 释放某键

  1. pyautogui.keyUp('ctrl')
  2. pyautogui.keyUp('v')

4 组合键

  1. pyautogui.hotkey('ctrl','v')

5 输入内容

:::info interval:间隔时间
Enter:执行回车 :::

  1. pyautogui.typewrite('python',interval=1)
  2. 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 模块 :::

  1. img = pyautogui.screenshot()
  2. img = pyautogui.screenshot(region=(0,0,100,100))
  3. img.save('screenshot.png')

2 获取像素RGB元祖

  1. img = pyautogui.screenshot()
  2. rgb = img.getpixel((500,500))
  3. print(rgb)

3 像素匹配

  1. match = pyautogui.pixelMatchesColor(500,500,rgb)
  2. print(match)

4 图像识别

1 取图片在屏幕相应位置的初始坐标与宽度高度

  1. location = pyautogui.locateOnScreen('test.png')
  2. location2 = pyautogui.locateOnScreen('test.png', region=(0, 0, 1920, 1080))
  3. locations = pyautogui.locateAllOnScreen('test.png')
  4. print(location)
  5. print(location2)
  6. print(list(locations))

2 取图片在屏幕相应位置的中心点

  1. centerLocation = pyautogui.locateCenterOnScreen('test.png')
  2. print(centerLocation)

3 图片匹配

:::info 返回找到 needleImagehaystackImage 里面的坐标,如果没找到返回None
grayscale: 灰度值匹配,去色对比 :::

  1. location = pyautogui.locate(needleImage='wx.png', haystackImage='wx3.png', grayscale=True)
  2. print(location)

5 获取 中心点

  1. center = pyautogui.center(location)
  2. print(center)

6 坐标是否在屏幕

  1. onscreen=pyautogui.onScreen(100,-1)
  2. print(onscreen)

7 信息框


1 确认框

  1. confirm = pyautogui.confirm(text='是否继续?',title='确认框',buttons=('是','否'))
  2. print(confirm)

2 警示框

  1. alert = pyautogui.alert(text='测试成功',title='警示框',button='确定')
  2. print(alert)

3 输入框

  1. prompt = pyautogui.prompt(text='请输入账号:',title='登录账号')
  2. print(prompt)

4 密码框

  1. password = pyautogui.password(text='请输入密码:',title='登录密码',mask='*')
  2. print(password)