依赖包
python
复制代码
import time
import pyautogui
import pyperclip
import os
import psutil
from pywinauto.application import Application
睡眠:
python
复制代码
pyautogui.sleep(1)
鼠标事件:
python
复制代码
pyautogui.moveTo(100, 100, duration=0.25)
pyautogui.click(100, 100, duration=0.25)
pyautogui.click(10,10) # 鼠标点击指定位置,默认 PRIMARY 键
pyautogui.click(10,10,button='left') # 单击左键
pyautogui.click(1000,300,button='right') # 单击右键
pyautogui.click(1000,300,button='middle') # 单击中键
## 为了更方便,下面的函数包装了click,不需要传参 button
pyautogui.leftClick(10,10) # 单击左键
pyautogui.rightClick(10,10) # 单击右键
pyautogui.middleClick(10,10) # 单击中键
# 双击鼠标
pyautogui.doubleClick(10,10)
# 三击鼠标
pyautogui.tripleClick(10,10)
# 按下 & 释放
pyautogui.mouseDown() # 鼠标按下
pyautogui.mouseUp() # 鼠标释放
# 拖动鼠标
pyautogui.dragTo(x, y) 绝对位置
pyautogui.drag(x, y) 相对位置
# 滚动鼠标 滚动发生在鼠标的当前位置。 传递正整数表示向上滚动, 传递负整数表示向下滚动。
pyautogui.scroll(-100)
pyautogui.hscroll(100) # 向右滚动100
pyautogui.hscroll(-100) # 向左滚动100
# 按键操作 press
pyautogui.press('enter', 5) # 连按5次回车
# 按下 & 释放
pyautogui.keyDown('A') : 模拟按键按下;
pyautogui.keyUp('A') : 模拟按键释放;
pyautogui.keyDown('ctrl')
pyautogui.keyDown('c')
pyautogui.keyUp('c')
pyautogui.keyUp('ctrl')
pyautogui.hotkey('ctrl', 'c')
杀死进程:
python
复制代码
def kill_process(process_name):
for proc in psutil.process_iter(['pid', 'name']):
if proc.info['name'] == process_name:
try:
proc.kill()
print(f"Process {process_name} with PID {proc.info['pid']} has been terminated.")
except psutil.NoSuchProcess:
print(f"Process {process_name} with PID {proc.info['pid']} no longer exists.")
except psutil.AccessDenied:
print(f"Access denied to terminate process {process_name}.")
# 使用
kill_process('AliWorkbench.exe')
获取所有窗口的名称:
python
复制代码
titles = pyautogui.getAllTitles()
print("titles: ", titles)
显示窗口:
python
复制代码
def make_window_visible(title):
try:
# 尝试连接到已运行的应用程序
app = Application(backend="uia").connect(title=title, timeout=10)
window = app.window(title=title)
# 确保窗口是可见的并且处于最前面
if not window.is_active():
window.set_focus() # 设置焦点,这通常会将窗口带到前台
if not window.is_visible():
window.show() # 显示窗口(如果它被最小化或隐藏)
print(f"Window '{title}' has been made visible and active.")
except Exception as e:
print(f"Failed to make window '{title}' visible: {e}")
# make_window_visible('微信')
根据图片找元素:
python
复制代码
# 这里只返回一个
def close_dialog():
try:
close_icon_center = pyautogui.locateCenterOnScreen('close_icon.png', grayscale=True, confidence=0.9)
pyautogui.sleep(0.01)
print("弹框关闭图标-坐标:", close_icon_center)
if close_icon_center:
pyautogui.leftClick(close_icon_center.x, close_icon_center.y)
pyautogui.sleep(0.01)
except pyautogui.ImageNotFoundException as ee:
print("没有发现弹框关闭图标")
# 多个的操作
down_select_icon_centers = list(pyautogui.locateAllOnScreen('down_select_icon.png', grayscale=True, confidence=0.9))
print("下拉箭头长度len:", len(down_select_icon_centers))
pyautogui.leftClick(pyautogui.center(down_select_icon_centers[1]).x, pyautogui.center(down_select_icon_centers[1]).y)
输入文字:
python
复制代码
# 这个可能会受到输入法的影响
pyautogui.write()
def paste_str(str1):
pyperclip.copy(str1)
pyautogui.hotkey('ctrl', 'v')
# pyperclip.copy('') # 重置粘贴板内容为空
# pyperclip.paste() 这是粘贴
打开应用:
python
复制代码
# 打开
Application().start("D:/Program Files/AliWorkbench/AliWorkbench.exe")
# 显示
make_window_visible('千牛工作台')
# 根据windows窗口名称获取窗口,并最大化
chat_window = pyautogui.getWindowsWithTitle('千牛工作台')[0]
chat_window.maximize()
# win.maximize() # 最大化
# win.minimize() # 最小化
# win.restore() # 如果是最大/小化,则恢复成正常状态
# win.close()