python库--pyautogui————windows模拟鼠标键盘、图像自动匹配、按钮弹窗

python库--pyautogui------------windows模拟鼠标键盘、图像自动匹配、按钮弹窗


一、模拟鼠标

鼠标移动

1s内匀速移动到坐标(100, 200)处

python 复制代码
pyautogui.moveTo(100, 200, duration=1)

1s内相对右移100,下移200

python 复制代码
pyautogui.moveRel(100, 200, duration=1)

鼠标点击

按下、松开

python 复制代码
pyautogui.mouseDown()
pyautogui.mouseUp()

对坐标(100, 200)点击3次左键,间隔为1s

python 复制代码
pyautogui.click(100, 200, clicks=3, interval=1, button='right')

滚轮向上滚100

python 复制代码
pyautogui.scroll(100)

二、模拟键盘

输入字符串s,每个字符输入间隔为0.5s

python 复制代码
pyautogui.typewrite(s, interval=0.5)

按键(键值在文章最下方)

按下、松开回车

python 复制代码
pyautogui.keyDown('enter')
pyautogui.keyUp('enter')

按顺序点击

python 复制代码
pyautogui.press(['enter', 'shift', 'tab'])

点击组合键

python 复制代码
pyautogui.hotkey('ctrl', 'v')

三、图像自动匹配

需要安装opencv库(pip install opencv-python)

获取图片在当前屏幕的所在位置的中心坐标,confidence为精度

python 复制代码
x, y = pyautogui.locateCenterOnScreen(r'.\**.png', confidence=0.9)

四、弹窗

带有一个按钮的弹窗,点击按钮后会返回button中的值(string)

python 复制代码
a = pyautogui.alert(text='', title='', button='abc')

带有多个按钮的弹窗,点击按钮后会返回对应buttons的值(string)

python 复制代码
a = pyautogui.confirm(text='', title='', buttons=['a', 'b', 'c'])

可输入内容的弹窗,自带OK与Cancel按钮,点击OK会返回输入的值,点击Cancel会返回None

python 复制代码
a = pyautogui.prompt(text='', title='', default='')

用于输入密码的弹窗,输入值会用mask中的字符代为显示,自带OK与Cancel按钮,点击OK会返回输入的值,点击Cancel会返回None

python 复制代码
a = pyautogui.password(text='', title='', mask='*')

五、按键名称

'a', 'b', 'c', ..., 'z'

'0', '1', '2', ..., '9'

'f1', 'f2', ..., 'f12'

'enter', 'esc', 'shift', 'shiftleft', 'shiftright', 'ctrl', 'ctrlleft', 'ctrlright', 'alt', 'altleft', 'altright', 'tab', 'capslock'

'up', 'down', 'left', 'right', 'home', 'end', 'pagedown', 'pageup'

'backspace', 'delete', 'insert'

'space', 'enter', 'esc'

'', '~', '!', '@', '#', '$', '%', '^', '&', '*', '(', ')', '-', '_', '=', '+', '[', '{', ']', '}', '', '|', ';', ':', '"', ',', '<', '.', '>', '/', '?'

'numlock', 'num0', 'num1', ..., 'num9', 'numdivide', 'nummultiply', 'numsubtract', 'numadd', 'numdecimal'

相关推荐
程序员三藏16 小时前
Selenium无法定位元素的几种解决方案
自动化测试·软件测试·python·selenium·测试工具·职场和发展·测试用例
前端小趴菜~时倾16 小时前
自我提升-python爬虫学习:day04
爬虫·python·学习
小罗和阿泽16 小时前
接口测试系列 接口自动化测试 pytest框架(三)
开发语言·python·pytest
猿界零零七1 天前
pip install mxnet 报错解决方案
python·pip·mxnet
不只会拍照的程序猿1 天前
《嵌入式AI筑基笔记02:Python数据类型01,从C的“硬核”到Python的“包容”》
人工智能·笔记·python
Jay_Franklin1 天前
Quarto与Python集成使用
开发语言·python·markdown
Oueii1 天前
Django全栈开发入门:构建一个博客系统
jvm·数据库·python
2401_831824961 天前
使用Fabric自动化你的部署流程
jvm·数据库·python
njidf1 天前
Python日志记录(Logging)最佳实践
jvm·数据库·python
@我漫长的孤独流浪1 天前
Python编程核心知识点速览
开发语言·数据库·python