【测试】pyautogui的安装、鼠标操作、键盘操作、交互弹窗、截图定位

1.pyautogui

pyautogui是用来做GUI桌面应用自动化的Python包,功能类似于按键精灵,可以实现控制鼠标、键盘、消息框、截图、定位功能,支持跨平台。不过也有缺点,比如说不支持中文输入(一般配合pyperclip解决此问题)

2.安装
# Windows
pip install pyautogui -i https://pypi.tuna.tsinghua.edu.cn/simple

# Mac
pip install pyobjc-core
pip install pyobjc
pip install pyautogui

# Linux
#sudo apt-get install scrot python3-tk python3-dev
pip install python3-xlib
pip install pyautogui
3.鼠标操作

以屏幕左上角的为原点,向右为x轴正向,向下为y轴正向,单位是像素,通过(x,y)确定位置

pyautogui.PAUSE=2  # 操作间隔时间
pyautogui.FAILSAFE = True  # 开启保护措施,鼠标移动到屏幕左上角会中断程序
pyautogui.size()  # 返回屏幕大小
pyautogui.position()  # 返回鼠标的位置
pyautogui.onScreen(x, y)  # 判断坐标是否在屏幕内
pyautogui.moveTo(x, y)  # 把鼠标移动到某个位置(绝对位置)
pyautogui.move(x,y)  # 把鼠标移动到某个位置(相对位置)
pyautogui.click()  # 单击鼠标左键,可以传入位置、次数、间隔等参数
pyautogui.rightClick()  # 单击鼠标右键
pyautogui.middleClick()	  # 单击鼠标中键
pyautogui.doubleClick()  # 双击鼠标左键
pyautogui.dragTo(x, y, button='left')  # 拖动到某个位置(绝对),button可选left、middle、right
pyautogui.drag(x, y, time)  # 拖动到某个位置(相对)
pyautogui.mouseDown()  # 鼠标压下
pyautogui.mouseUp()   # 鼠标抬起
pyautogui.scroll()  # 鼠标滚动,正负数
import pyautogui

print("屏幕分辨率:", pyautogui.size())
print("鼠标位置:", pyautogui.position())
pyautogui.PAUSE = 2  # 设置每次执行暂停2秒

pyautogui.moveTo(500, 500)  # 把鼠标移动到(500,500)
pyautogui.rightClick()  # 在原地按鼠标右键
pyautogui.click(clicks=2, interval=0.3)  # 双击2次,间隔是0.3秒
pyautogui.scroll(-500)  # 向下滚动500像素
4.键盘操作
pyautogui.KEYBOARD_KEYS  # 可以控制的按键名称
pyautogui.write(msg)  # 输入字符,不支持中文
pyperclip.copy(msg)  # 复制内容msg,内容可设置为中文等
pyautogui.write([key1,key2])  # 键入给定键字符串,只能是英文
pyautogui.press(key)  # 按下并释放给定键
pyautogui.keyDown(key)  # 按住按键
pyautogui.keyUp(key)  # 松开按键
pyautogui.hotkey('ctrl', 'v')  # 模拟按顺序按下给定键字符串,然后以相反的顺序释放
import pyautogui
import pyperclip

pyautogui.PAUSE = 1  # 每次执行暂停一秒
print(pyautogui.KEYBOARD_KEYS)  # 打印支持的键

pyautogui.write("hello world")  # 在光标激活处输入文本

pyautogui.press('enter')  # 回车

pyautogui.keyDown('shift')  # 按下shift键
pyautogui.press('3')  # 按3,实际上输入的是#号注释符
pyautogui.keyUp('shift')  # 松开shift键

pyperclip.copy("这个是中文文本")  # 复制文本到剪切板
pyautogui.hotkey('ctrl', 'v')  # 按下ctrl+v快捷键粘贴微博

# 建议光标移动到注释这里,把输入法调成英文状态

5.信息弹窗
pyautogui.alert()  # 简单提示消息框
pyautogui.confirm()  # 多按钮消息框
pyautogui.prompt()  # 明文输入消息框
pyautogui.password()  # 密文输入消息框
import pyautogui

# 警告框,只有一个确定按钮
pyautogui.alert(text='警告内容', title='警告框', button='OK')

# 确认框,可以有多个按钮
result = pyautogui.confirm(text='请选择', title='确认框', buttons=['确定', 'Cancel'])
print(result)

# 可以输入内容的提示框
content = pyautogui.prompt(text='', title='请输入', default='')
print(content)

# 可以输入文本,以密文符号替代显示
content = pyautogui.password(text='', title='', default='', mask='*')
print(content)

6.截图定位
pyautogui.screenshot(path,regon)  # 截图,region是一个元组,不传则为全屏
pyautogui.locateOnScreen(region,grayscale)  # 从屏幕寻找图片位置,返回一个4边距坐标
pyautogui.locateCenterOnScreen()  # 从屏幕寻找图片位置,返回中心点坐标
import pyautogui

pyautogui.screenshot("./images/1.png", region=(0, 0, 500, 500))  # region是一个元组,不传则为全屏
rect = pyautogui.locateOnScreen('./images/1.png')  # 根据某张图片匹配定位元素位置,返回一个区域
if rect:
    print(type(rect))  # 是pyscreeze.Box类型
    print(rect[0], rect[1], rect[2], rect[3])
    x, y = pyautogui.center(rect)
    print()  # 获取rect的中心坐标

point = pyautogui.locateCenterOnScreen('./images/1.png')
if point:  # point不为None才往下操作
    print(type(point))  # 是pyscreeze.Point类型
    x, y = point  # 拆包
    print(x, y)

修改可以提供定位效率的参数

import pyautogui

pyautogui.screenshot("./images/1.png", region=(0, 0, 500, 500))  # region是一个元组,不传则为全屏

rect = pyautogui.locateOnScreen('./images/1.png', confidence=0.6)  # 调整精度,confidence参数需要opencv-python库支持
print(rect)
rect = pyautogui.locateOnScreen('./images/1.png', region=(0, 0, 1000, 1000))  # 缩小定位范围
print(rect)
rect = pyautogui.locateOnScreen('./images/1.png', grayscale=True)  # 使用灰度缩放
print(rect)

7.案例

我们可以根据上面的知识点做个小小的例子,就是,自动打开记事本,然后输入内容,再保存

import pyautogui
import pyperclip
import os
import time

def write_word(texts="", interval=0.1):
    for t in texts:
        # pyautogui.write(t)  # 不支持中文,所以我们不用这个方法
        pyperclip.copy(t)
        pyautogui.hotkey("ctrl", "v")
        time.sleep(interval)

def main():
    res = os.system("start notepad")
    if res != 0:
        print("打开记事本失败")
        return
    time.sleep(2)  # 一定要暂停一下等待打开记事本
    pyautogui.PAUSE = 1
    # 先截好记事本的图存为notepad.jpg
    x, y = pyautogui.locateCenterOnScreen("./images/notepad.jpg", confidence=0.6, grayscale=True)
    pyautogui.click(x, y)
    texts = "hello world,"
    write_word(texts, 0.1)
    pyautogui.hotkey("ctrl", "s")
    write_word("自动文档.txt", 0.1)
    pyautogui.press("enter")
    pyautogui.press("y")
    pyautogui.hotkey("alt", "f4")

if __name__ == '__main__':
    main()

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

冰冷的希望

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值