详解pyautogui模块

时间:2022-11-05 13:06:56

一.安装

pip install pyautogui

或者

pip install -i  https://pypi.tuna.tsinghua.edu.cn/simple pyautogui

二.全局设置

s为秒数,不填默认0.1s。表示所有pyautogui相关的指令运行后等待s秒在运行下一条。相当于全局等待

pyautogui.PAUSE=s

防止操作失控,默认就是True,通过FAILSAFE_POINTS来设置坐标,默认(0,0)

详解pyautogui模块

也可手动更改

pyautogui.FAILSAFE = True  # 防止操作失控,设置鼠标移动某点是抛异常
pyautogui.FAILSAFE_POINTS = [(100, 0)] # 鼠标移动到(100,0)位置时抛出异常

三.获取电脑分辨率

x, y = pyautogui.size()
print(x, y)

四.鼠标事件

1.pyautogui.click():单击点击事件

pyautogui.click(x=None, y=None, clicks=1, interval=0.0, button=PRIMARY, duration=0.0, tween=linear)
# x,y:为点击的位置坐标,以电脑左上角为原点
# clicks:为点击次数,默认为1
# interval:多次点击间的间隔(比如双击,第一次点击完后,等待设置的秒数在执行下一次点击)
# button:设置左击还是右击,默认左击
# duration:设置执行的时间
# tween:鼠标移动到指定位置的规律

button:设置鼠标点击项(left:左击 ,right:右击 ,middle:中击)

除了click()方法可以单击外,pyautogui还提供了专用于左/右/中击的方法。

pyautogui.leftClick()  # 左键单击
pyautogui.rightClick()  # 单击右键
pyautogui.middleClick()  # 中键单击

leftClick()的用法等同于click(button='left')

rightClick()的用法等同于click(button='right')

middleClick()的用法等同于click(button='middle')

2.pyautogui.doubleClick():双击点击事件

pyautogui.doubleClick()  # 双击,默认左双击
# x,y:为点击的位置坐标,以电脑左上角为原点
# clicks:为点击次数,默认为1
# interval:两次点击间的间隔(比如双击,第一次点击完后,等待设置的秒数在执行下一次点击)
# button:设置左击还是右击,默认左击
# duration:设置执行的时间
# tween:鼠标移动到指定位置的规律

3.pyautogui.tripleClick():三击点击事件

pyautogui.tripleClick()  # 单击三次,默认左击

4.鼠标移动

1)pyautogui.moveTo():以坐标为原点移动到指定的坐标上

pyautogui.moveTo(x=None, y=None, duration=0.0, tween=pyautogui.easeInOutElastic)  # 鼠标移动到坐标位置,以电脑屏幕左上角为原地定位
# x,y:为移动的位置坐标,以电脑左上角为原点
# button:设置左击还是右击,默认左击
# duration:设置执行的时间
# tween:鼠标移动到指定位置的规律,默认匀速直线运行(linear)

这个介绍下tween参数的值共有哪些。

easeInQuad  详解pyautogui模块

 

easeOutQuad 
easeInOutQuad 
easeInCubic 
easeOutCubic 
easeInOutCubic 
easeInQuart 
easeOutQuart 
easeInOutQuart 
easeInQuint 
easeOutQuint 
easeInOutQuint 
easeInSine 
easeOutSine 
easeInOutSine 
easeInExpo 
easeOutExpo 
easeInOutExpo 
easeInCirc 
easeOutCirc 
easeInOutCirc 
easeInElastic 
easeOutElastic 
easeInOutElastic 
easeInBack 
easeOutBack 
easeInOutBack 
easeInBounce 
easeOutBounce 
easeInOutBounce 

左边为属性,右侧为运行的方式(速度+轨迹),图片来源于’https://blog.csdn.net/S_clifftop?type=blog‘如有侵权,请告知删除。

pyautogui.position():可获取当前鼠标坐标

x, y = pyautogui.position()  # 获取鼠标当前位置
print(x,y)

2)pyautogui.move():以鼠标当前位置移动x,y个像素

pyautogui.move(x=None, y=None, duration=0.0, tween=pyautogui.easeInQuad)  # 在当前位置移动,以当前位置为坐标

该方法与moveTo()的区别在于开始移动的位置不一样,x,y参数是以当前位置相对移动的像素,可正值可负值。

5.按下与松开

pyautogui.mouseDown():按下鼠标

pyautogui.mouseDown(x=None, y=None, button='', duration=0.0)  # 按下鼠标,填写坐标的话,就是移动到某个坐标下按下
# x,y为移动到某点按下,不填的话默认当前位置按下
# button按下的点击项,默认左按下
# duration整体指令运行的时间

pyautogui.mouseUp():松开鼠标

pyautogui.mouseUp()  # 释放鼠标

我们在做登录验证滑块的时候,就可以用这两个方法+move()去拖拽滑块,并且还不会被检测出来

6.鼠标拖拽

pyautogui.dragRel():在鼠标当前位置点击后相对移动

pyautogui.dragRel(xOffset=20, yOffset=100, duration=3, tween=pyautogui.easeInQuad, button='left')  # 在鼠标当前位置点击后相对移动,结束后自动松开
# xOffset,yOffset为移动的距离
# duration整个移动的耗时
# tween移动的速度+轨迹
# button设置点击项,默认左击

pyautogui.drag():该方法与dragRel()用法及效果一致,这里不做多余介绍了

pyautogui.dragTo():从当前位置拖拽到指定坐标轴

pyautogui.dragTo(x=None, y=None, duration=0.0, tween=pyautogui.easeInQuad, button="left")  # 鼠标从当前位置拖拽到指定坐标
# x,y为坐标,鼠标移动到指定的坐标(x,y)
# duration整个移动的耗时
# tween移动的速度+轨迹
# button设置点击项,默认左击

五.键盘事件

1.按下与松开

pyautogui.keyDown('') :按下某键盘

pyautogui.keyDown('shift')  # 按下shift键

pyautogui.keyUp('') :松开某键盘

pyautogui.keyUp('shift')  # 松开shift键

常用的键盘表示(自己脑补用法吧)

KEY_NAMES = [
    "\t", # tab
    "\n",  # 回车
    "\r",
    " ",
    "!",
    '"',
    "#",
    "$",
    "%",
    "&",
    "'",
    "(",
    ")",
    "*",
    "+",
    ",",
    "-",
    ".",
    "/",
    "0",
    "1",
    "2",
    "3",
    "4",
    "5",
    "6",
    "7",
    "8",
    "9",
    ":",
    ";",
    "<",
    "=",
    ">",
    "?",
    "@",
    "[",
    "\\",
    "]",
    "^",
    "_",
    "`",
    "a",
    "b",
    "c",
    "d",
    "e",
    "f",
    "g",
    "h",
    "i",
    "j",
    "k",
    "l",
    "m",
    "n",
    "o",
    "p",
    "q",
    "r",
    "s",
    "t",
    "u",
    "v",
    "w",
    "x",
    "y",
    "z",
    "{",
    "|",
    "}",
    "~",
    "accept",
    "add",
    "alt",
    "altleft",
    "altright",
    "apps",
    "backspace",
    "browserback",
    "browserfavorites",
    "browserforward",
    "browserhome",
    "browserrefresh",
    "browsersearch",
    "browserstop",
    "capslock",
    "clear",
    "convert",
    "ctrl",
    "ctrlleft",
    "ctrlright",
    "decimal",
    "del",
    "delete",
    "divide",
    "down",
    "end",
    "enter",
    "esc",
    "escape",
    "execute",
    "f1",
    "f10",
    "f11",
    "f12",
    "f13",
    "f14",
    "f15",
    "f16",
    "f17",
    "f18",
    "f19",
    "f2",
    "f20",
    "f21",
    "f22",
    "f23",
    "f24",
    "f3",
    "f4",
    "f5",
    "f6",
    "f7",
    "f8",
    "f9",
    "final",
    "fn",
    "hanguel",
    "hangul",
    "hanja",
    "help",
    "home",
    "insert",
    "junja",
    "kana",
    "kanji",
    "launchapp1",
    "launchapp2",
    "launchmail",
    "launchmediaselect",
    "left",
    "modechange",
    "multiply",
    "nexttrack",
    "nonconvert",
    "num0",
    "num1",
    "num2",
    "num3",
    "num4",
    "num5",
    "num6",
    "num7",
    "num8",
    "num9",
    "numlock",
    "pagedown",
    "pageup",
    "pause",
    "pgdn",
    "pgup",
    "playpause",
    "prevtrack",
    "print",
    "printscreen",
    "prntscrn",
    "prtsc",
    "prtscr",
    "return",
    "right",
    "scrolllock",
    "select",
    "separator",
    "shift",
    "shiftleft",
    "shiftright",
    "sleep",
    "space",
    "stop",
    "subtract",
    "tab",
    "up",
    "volumedown",
    "volumemute",
    "volumeup",
    "win",
    "winleft",
    "winright",
    "yen",
    "command",
    "option",
    "optionleft",
    "optionright",
]

2.输入内容

pyautogui.press(' '):输入内容

pyautogui.keyDown('shift')  # 按下shift键
pyautogui.press("a")  # 打印A
pyautogui.keyUp('shift')  # 松开shift键

3.缓慢输入

pyautogui.typewrite():缓慢输入

pyautogui.typewrite("message", interval=2)  # 缓慢输入
# message:为输入的内容
# interval:设置每两个字符输入的时间间隔,单位s

4.多键组合使用

pyautogui.hotkey():多键组合使用(如以下设置全选)

pyautogui.hotkey('ctrl', 'a')  # 多键组合使用

六.弹框

pyautogui.alert():alert弹框

a = pyautogui.alert(text='test', title='pyautogui')  # alert弹框
# text:弹框内容
# title:弹框标题
print(a)  # 打印ok

当弹框运行时程序会暂停往下走,点击取消/确定时才会继续往下执行

pyautogui.confirm():confirm弹框

a = pyautogui.confirm(text='test', title='pyautogui')  # alert弹框,点击确定返回ok,点击取消返回cancel
# text:弹框内容
# title:弹框标题
print(a)

pyautogui.password():password弹框

pyautogui.prompt():prompt弹框

七.截图

pyautogui.screenshot():截图

img = pyautogui.screenshot()  # 默认截取全屏
img.save("./img/gui.png")  # 截取后的图片保存位置

如果我们想要截取某坐标的图片,可以添加region元组

region = (1690, 144, 100, 20)  # 前两位为x,y坐标,第三位为截取图片的宽,第四位为截取图片的高
img = pyautogui.screenshot(region=region)  
img.save("./img/gui.png")

pyautogui.locateCenterOnScreen():根据图片获取在当前屏幕匹配的第一个中心坐标

x, y = pyautogui.locateCenterOnScreen("./img/gui.png")
print(x,y)  # 打印匹配的x,y坐标

若无匹配结果,则返回None,报cannot unpack non-iterable NoneType object异常

除了locateCenterOnScreen(),还有以下用法也可获取匹配图片坐标

a = pyautogui.locateOnScreen("./img/gui.png")  # 返回匹配图片的左上角坐标+宽+高
x, y = pyautogui.center(a)  # 或者中心坐标位置
print(x, y)

pyautogui.locateAllOnScreen():根据图片获取在当前屏幕匹配的所有坐标及宽高

img = pyautogui.locateAllOnScreen("./img/gui.png")  # 返回页面所有可匹配的图片,返回一个生成器(左,顶,宽,高)
for i in img:
    x, y, w, h = i
    print(x, y)

这里需要注意的是,返回的结果是一个生成器,需for遍历输出

八.页面滚动

pyautogui.scroll():垂直滚动,负数为向下滚动,正数为向上滚动

pyautogui.scroll(-500)

如果想要缓慢滚动,可以加个for循环

for i in range(1, 100):
    pyautogui.scroll(-i) 

pyautogui.hscroll() :水平滚动

详解pyautogui模块

 这个不做多余介绍了,只有linux系统支持

pyautogui.vscroll() :垂直滚动,负下正上

pyautogui.vscroll(-100)  # 垂直滚动,负下正

















文章来源:https://www.cnblogs.com/lihongtaoya/ ,请勿转载