import pyautogui
size = pyautogui.size() # 屏幕大小
print(pyautogui.position()) # 鼠标位置
print(pyautogui.onScreen(100, 100)) # 判断点是否在屏幕内
pyautogui.moveTo(size.width / 2, size.height / 2, duration = .5) # 鼠标移动到屏幕中央
异常处理
try: # 当自动化异常退出
while True:
except KeyboardInterrupt:
print('\nExit.')
模拟点击
import pyautogui
import time
time.sleep(2) # 系统准备时间
# 鼠标移到参考图片中央并点击,模拟点击帮助菜单及子菜单
help_pos = pyautogui.locateOnScreen('btn_help.png')
goto_pos = pyautogui.center(help_pos)
pyautogui.moveTo(goto_pos, duration = 1)
pyautogui.click()
pyautogui.moveRel(None, 650, duration = 1) # 鼠标相对移动
pyautogui.click()
模拟输入
import pyautogui
import time
time.sleep(2) # 系统准备时间
pyautogui.click(button = 'left') # 打开编辑器,模拟输入
pyautogui.typewrite('I like Python.') # 瞬间输入
pyautogui.typewrite('\nI like Python too.', .25) # 逐字输入
pyautogui.typewrite(['enter', 'g', 'o', 'o', 'd', 'left', 'left', 'left', 'backspace', 'G', 'end', '.'], .25) # 字节输入并修改
pyautogui.PAUSE = .5 # 动作间间隔.5秒
pyautogui.keyDown('alt') # 按下alt键
pyautogui.press('a') # 按下a键,全选
pyautogui.press('c') # 按下c键,复制
pyautogui.keyUp('alt') # 松开alt键
pyautogui.hotkey('alt', 'v') # 组合键,粘贴
网友评论