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")# 打印Apyautogui.keyUp('shift')# 松开shift键3.缓慢输入pyautogui.typewrite():缓慢输入
pyautogui.typewrite("message", interval=2)# 缓慢输入# message:为输入的内容# interval:设置每两个字符输入的时间间隔,单位s4.多键组合使用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模块】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 = iprint(x, y)这里需要注意的是 , 返回的结果是一个生成器,需for遍历输出
八.页面滚动pyautogui.scroll():垂直滚动 , 负数为向下滚动,正数为向上滚动pyautogui.scroll(-500)如果想要缓慢滚动,可以加个for循环
for i in range(1, 100):pyautogui.scroll(-i) pyautogui.hscroll() :水平滚动
![详解pyautogui模块](http://img.zhejianglong.com/231019/131SQ448-2.png)
文章插图
这个不做多余介绍了,只有linux系统支持
推荐阅读
- 2022-11-4 VideoPipe可视化视频结构化框架新增功能详解
- Istio Ambient Mesh七层服务治理图文详解
- 详解AQS中的condition源码原理
- FHQ Treap 详解
- springboot项目整合-注册功能模块开发
- 一 UBOOT编译--- make xxx_deconfig过程详解
- 图文超详解 G1 垃圾收集器深入剖析
- 优雅的写好Vue项目代码 — 路由拆分、Vuex模块拆分、element按需加载
- 一 Abp.Zero 手机号免密登录验证与号码绑定功能的实现:验证码模块
- 图文全面详解 Kafka 架构和原理机制