使用 PyAutoGUI 自动化操作小程序

本文将演示如何使用 Python 的 PyAutoGUI 库,自动打开并操作小程序,例如点击按钮等。

示例代码:

import os
import time
import pyautogui

# 打开桌面上的小程序快捷方式
shortcut_path = r'C:\Users\Administrator\Desktop\国开照片采集工具.lnk'
os.startfile(shortcut_path)

# 等待小程序打开
time.sleep(1)

# 在打开的小程序中找到并点击'我的照片查询'
my_photos_query_text = '我的照片查询'
my_photos_query_position = pyautogui.locateOnScreen('my_photos_query.png')
if my_photos_query_position:
    my_photos_query_center = pyautogui.center(my_photos_query_position)
    pyautogui.click(my_photos_query_center)
else:
    print('未找到'我的照片查询'字样')

# 等待页面加载
time.sleep(1)

# 在打开的小程序中找到并点击'考试照片查询'
exam_photos_query_text = '考试照片查询'
exam_photos_query_position = pyautogui.locateOnScreen('exam_photos_query.png')
if exam_photos_query_position:
    exam_photos_query_center = pyautogui.center(exam_photos_query_position)
    pyautogui.click(exam_photos_query_center)
else:
    print('未找到'考试照片查询'字样')

解决 pyscreeze 模块缺少 locateOnWindow 属性的错误:

该错误通常是因为你使用的 pyscreeze 版本过低导致缺少locateOnWindow属性。解决方法是更新 pyscreeze 模块。

更新 pyscreeze:

pip install --upgrade pyscreeze

卸载并重新安装 pyscreeze:

pip uninstall pyscreeze
pip install pyscreeze

更新或重新安装完 pyscreeze 后,再运行代码应该就不会报错了。

注意:

  • 代码中的 my_photos_query.pngexam_photos_query.png 是你需要提前准备的图片文件,这些文件需要包含小程序中“我的照片查询”和“考试照片查询”按钮的截图。
  • 你可以根据实际情况修改代码中的快捷方式路径、按钮名称和图片文件路径。
  • 使用 PyAutoGUI 进行自动化操作需要确保小程序窗口处于最前面,并且没有被其他窗口遮挡。
Python 自动化操作小程序:使用 PyAutoGUI 点击按钮

原文地址: https://www.cveoy.top/t/topic/bsDk 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录