python PyAutoGUI 模拟鼠标键盘操作和截屏功能
作者:lilongsy 时间:2022-11-12 08:16:41
简介
一款跨平台/无依赖的自动化测试工具,目测只能控制鼠标/键盘/获取屏幕尺寸/弹出消息框/截屏。
安装
pip install pyautogui
鼠标键盘控制
>>> import pyautogui
>>> screenWidth, screenHeight = pyautogui.size()
>>> currentMouseX, currentMouseY = pyautogui.position()
>>> pyautogui.moveTo(100, 150)
>>> pyautogui.click()
>>> pyautogui.moveRel(None, 10) # move mouse 10 pixels down
>>> pyautogui.doubleClick()
>>> pyautogui.moveTo(500, 500, duration=2, tween=pyautogui.tweens.easeInOutQuad) # use tweening/easing function to move mouse over 2 seconds.
>>> pyautogui.typewrite('Hello world!', interval=0.25) # type with quarter-second pause in between each key
>>> pyautogui.press('esc')
>>> pyautogui.keyDown('shift')
>>> pyautogui.typewrite(['left', 'left', 'left', 'left', 'left', 'left'])
>>> pyautogui.keyUp('shift')
>>> pyautogui.hotkey('ctrl', 'c')
显示消息弹出框
>>> import pyautogui
>>> pyautogui.alert('This is an alert box.')
'OK'
>>> pyautogui.confirm('Shall I proceed?')
'Cancel'
>>> pyautogui.confirm('Enter option.', buttons=['A', 'B', 'C'])
'B'
>>> pyautogui.prompt('What is your name?')
'Al'
>>> pyautogui.password('Enter password (text will be hidden)')
'swordfish'
截屏
>>> import pyautogui
>>> im1 = pyautogui.screenshot()
>>> im1.save('my_screenshot.png')
>>> im2 = pyautogui.screenshot('my_screenshot2.png')
定位截屏
>>> import pyautogui
>>> button7location = pyautogui.locateOnScreen('button.png') # returns (left, top, width, height) of matching region
>>> button7location
(1416, 562, 50, 41)
>>> buttonx, buttony = pyautogui.center(button7location)
>>> buttonx, buttony
(1441, 582)
>>> pyautogui.click(buttonx, buttony) # clicks the center of where the button was found
参考
http://pyautogui.readthedocs.io/en/latest/index.html
https://github.com/asweigart/pyautogui
https://github.com/asweigart/sushigoroundbot
总结
以上所述是小编给大家介绍的python PyAutoGUI 模拟鼠标键盘操作和截屏功能,希望对大家有所帮助,如果大家有任何疑问欢迎给我留言,小编会及时回复大家的!
来源:https://blog.csdn.net/lilongsy/article/details/81607125
标签:python,PyAutoGUI,鼠标,键盘,截屏
0
投稿
猜你喜欢
pycharm new project变成灰色的解决方法
2023-10-29 20:29:23
解密ThinkPHP3.1.2版本之独立分组功能应用
2023-11-20 09:06:02
Python进阶篇之多线程爬取网页
2023-01-15 13:22:39
PyQt4实现下拉菜单可供选择并打印出来
2023-08-19 01:10:35
Java通过MySQL的加解密函数实现敏感字段存储
2024-01-27 12:08:25
关于JSON以及JSON在PHP中的应用技巧
2023-11-16 00:03:38
Python Pygame实战之实现经营类游戏梦想小镇代码版
2021-08-06 19:13:23
在php7中MongoDB实现模糊查询的方法详解
2023-09-04 12:30:00
Python进程间通信之共享内存详解
2022-01-06 05:47:10
做新产品?悠着点
2009-03-01 13:10:00
JavaScript中随机数方法 Math.random()
2024-04-10 13:56:30
教你如何在Mac上安装mysql数据库
2024-01-23 05:19:10
Python 内置变量和函数的查看及说明介绍
2021-06-11 16:12:14
python实现批量解析邮件并下载附件
2023-07-05 02:32:16
使用Python的Django框架结合jQuery实现AJAX购物车页面
2023-05-21 01:59:28
pandas object格式转float64格式的方法
2022-05-26 15:58:53
expdp 中ORA-39002、ORA-39070错误详解及解决办法
2024-01-19 16:37:47
Python中使用 Selenium 实现网页截图实例
2022-07-04 06:28:16
SQL语句练习实例之三——平均销售等待时间
2011-10-24 20:11:47
利用types增强vscode中js代码提示功能详解
2023-07-15 05:57:08