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.FAILSAFE = True # 防止操作失控,设置鼠标移动某点是抛异常
pyautogui.FAILSAFE_POINTS = [(100, 0)] # 鼠标移动到(100,0)位置时抛出异常
x, y = pyautogui.size()
print(x, y)
pyautogui.click(x=None, y=None, clicks=1, interval=0.0, button=PRIMARY, duration=0.0, tween=linear)
button:设置鼠标点击项(left:左击 ,right:右击 ,middle:中击)
除了click()方法可以单击外,pyautogui还提供了专用于左/右/中击的方法。
pyautogui.leftClick() # 左键单击
pyautogui.rightClick() # 单击右键
pyautogui.middleClick() # 中键单击
leftClick()的用法等同于click(button='left')
rightClick()的用法等同于click(button='right')
middleClick()的用法等同于click(button='middle')
pyautogui.doubleClick() # 双击,默认左双击
pyautogui.tripleClick() # 单击三次,默认左击
1)pyautogui.moveTo():以坐标为原点移动到指定的坐标上
pyautogui.moveTo(x=None, y=None, duration=0.0, tween=pyautogui.easeInOutElastic) # 鼠标移动到坐标位置,以电脑屏幕左上角为原地定位
这个介绍下tween参数的值共有哪些。
easeInQuad
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参数是以当前位置相对移动的像素,可正值可负值。
pyautogui.mouseDown():按下鼠标
pyautogui.mouseDown(x=None, y=None, button='', duration=0.0) # 按下鼠标,填写坐标的话,就是移动到某个坐标下按下
pyautogui.mouseUp():松开鼠标
pyautogui.mouseUp() # 释放鼠标
我们在做登录验证滑块的时候,就可以用这两个方法+move()去拖拽滑块,并且还不会被检测出来
pyautogui.dragRel():在鼠标当前位置点击后相对移动
pyautogui.dragRel(xOffset=20, yOffset=100, duration=3, tween=pyautogui.easeInQuad, button='left') # 在鼠标当前位置点击后相对移动,结束后自动松开
pyautogui.drag():该方法与dragRel()用法及效果一致,这里不做多余介绍了
pyautogui.dragTo():从当前位置拖拽到指定坐标轴
pyautogui.dragTo(x=None, y=None, duration=0.0, tween=pyautogui.easeInQuad, button="left") # 鼠标从当前位置拖拽到指定坐标
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",
]
pyautogui.press(' '):输入内容
pyautogui.keyDown('shift') # 按下shift键
pyautogui.press("a") # 打印A
pyautogui.keyUp('shift') # 松开shift键
pyautogui.typewrite():缓慢输入
pyautogui.typewrite("message", interval=2) # 缓慢输入
pyautogui.hotkey():多键组合使用(如以下设置全选)
pyautogui.hotkey('ctrl', 'a') # 多键组合使用
pyautogui.alert():alert弹框
a = pyautogui.alert(text='test', title='pyautogui') # alert弹框
print(a) # 打印ok
当弹框运行时程序会暂停往下走,点击取消/确定时才会继续往下执行
pyautogui.confirm():confirm弹框
a = pyautogui.confirm(text='test', title='pyautogui') # alert弹框,点击确定返回ok,点击取消返回cancel
print(a)
pyautogui.password():password弹框
pyautogui.prompt():prompt弹框
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")
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)
img = pyautogui.locateAllOnScreen("./img/gui.png") # 返回页面所有可匹配的图片,返回一个生成器(左,顶,宽,高)
for i in img:
x, y, w, h = i
print(x, y)
这里需要注意的是,返回的结果是一个生成器,需for遍历输出
pyautogui.scroll(-500)
如果想要缓慢滚动,可以加个for循环
for i in range(1, 100):
pyautogui.scroll(-i)
这个不做多余介绍了,只有linux系统支持
pyautogui.vscroll(-100) # 垂直滚动,负下正
文章来源:https://www.cnblogs.com/lihongtaoya/ ,请勿转载
手机扫一扫
移动阅读更方便
你可能感兴趣的文章