import pyautogui
import pyperclip
# 鼠标操作
# 启动自动防故障功能,鼠标放在左上角,抛出failSafeException异常。默认为True
pyautogui.FAILSAFE = True
# 在执行动作后暂停的秒数,只对pyautogui函数有用。为所有的PyAutoGUI函数增加延迟,默认延迟时间是0.1秒
pyautogui.PAUSE = 2
# 已桌面右上角为(0,0),鼠标点击此坐标处
# def click(x=None, y=None, clicks=1, interval=0.0, button=PRIMARY, duration=0.0, tween=linear, logScreenshot=None, _pause=True)
# x=None, 横坐标
# y=None, 纵坐标
# clicks=1, 鼠标点击几次(可以增加鼠标点击间隔时间,来根据页面加载依次点击)
# interval=0.0, 每次点击间隔时间
# button=PRIMARY, button有几个选项默认是左键,-``LEFT``, ``MIDDLE``, ``RIGHT``, ``PRIMARY``, or ``SECONDARY``.
# duration=0.0, 执行此次动作设定的时间
# tween=linear,
# logScreenshot=None,
# _pause=True
pyautogui.click(624, 149, clicks=2, interval=1, button='LEFT', duration=2)
# 左键单击
pyautogui.leftClick(133,255)
# 右键单击
pyautogui.rightClick(133,255)
# 这两个操作之间间隔的时间(秒)
pyautogui.sleep(2)
pyautogui.click(900,900)
# 鼠标的移动
# 移动到指定位置鼠标按下
pyautogui.mouseDown(133, 255, button='primary')
# 移动到指定位置鼠标松开
pyautogui.mouseUp(133, 255, button='primary')
# 绝对移动,把鼠标移动至屏幕的此坐标处
pyautogui.dragTo(133,255,2,button='left')
# 相对移动,当前鼠标位置的相对移动
pyautogui.dragRel(133,255,2,button='left')
# 鼠标滑轮滚动、有问题
# clicks(100)
pyautogui.scroll(100,x=(100,200),y=(100,500))
# 屏幕的大小,以坐标方式展示
screenWidth,screenHeight = pyautogui.size()
print(screenWidth,screenHeight)
print(pyautogui.size())
# 当前鼠标的坐标位置
currentMouseX,currentMouseY = pyautogui.position()
print(currentMouseX,currentMouseY)
print(pyautogui.position())
# 键盘操作
# pyautogui.press(keys, presses=1, interval=0.0)
# keys :要按下的键,
# presses=1 :次数
# interval=0.0 :间隔时间
pyautogui.press('enter')
pyautogui.click(200, 430)
pyautogui.press(['enter', 'a', 'b', '1'], 5, 1)
# 热键
pyautogui.hotkey('ctrl', 'a')
# 字符输入,暂不支持中文(中文可以用另外一种函数实现)
# typewrite('输入的字符', 间隔的时间)
pyautogui.typewrite('H12345', interval=0.1)
# 输入中文
pyperclip.copy('哈哈')
pyautogui.hotkey('ctrl', 'v')
pyautogui.typewrite(['a', 'b', 'c', 'left', 'backspace', 'enter', 'f1'], interval=1)
# 查看按键名称列表
pyautogui.KEYBOARD_KEYS
pyautogui.press('enter')
pyautogui.keyDown('ctrl')
pyautogui.keyUp('ctrl')
# 消息弹窗函数
pyautogui.alert('这个消息弹窗是文字+OK按钮')
pyautogui.confirm('这个消息弹窗是文字+OK+Cancel按钮')
pyautogui.prompt('这个消息弹窗是让用户输入字符串,单击OK')
pyautogui.prompt(title='提示框标题', text='文本提示内容~', default='输入框默认!')
''' 光标位置记录程序
import pyautogui
def mouse_record():
print('Press Ctrl-C to quit')
try:
while True:
x, y = pyautogui.position()
positionStr = 'X: {} Y: {}'.format(*[str(x).rjust(4) for x in [x, y]])
print(positionStr, end='')
print('\b' * len(positionStr), end='', flush=True)
except KeyboardInterrupt:
print('\n')
mouse_record()
'''
# 其他操作
# 截取屏幕上的图片('图片名称',region=(起始x,起始y,终x,终y)),图片默认放在当前目录下
pyautogui.screenshot('a.png',region=(100,200,300,400))
# 返回屏幕上第一个找到的图像实例中心的(x,y)坐标
print(pyautogui.locateCenterOnScreen('a.png'))
# Returns a list of Window objects for all visible windows.返回所有打开窗口的对象
pyautogui.getAllWindows()
# Returns a list of Window objects whose windows contain the point ``(x, y)``.
# 返回其窗口包含点“`(x,y)`”的窗口对象的列表。
pyautogui.getWindowsAt(100,300)
#返回当前活动窗口对象
pyautogui.getActiveWindow()
#返回当前活动对象的窗口标题
pyautogui.getActiveWindowTitle('title')
#返回所有的窗口标题
pyautogui.getAllTitles()
#根据标题找窗口,返回窗口对象
pyautogui.getWindowsWithTitle('title')
python自动化操作——PyAutogui库
最新推荐文章于 2024-04-30 19:24:04 发布