Python 自动化操作: 使用 pyautogui 点击桌面小程序按钮
使用 Python 自动点击桌面小程序按钮
本文介绍了如何使用 Python 的 pyautogui 库自动点击桌面小程序按钮。
代码示例
import subprocess
import pyautogui
import time
# 打开桌面上的小程序
program_path = r'C:\Users\Administrator\Desktop\国开照片采集工具.lnk'
subprocess.Popen(program_path)
# 等待小程序打开
time.sleep(5)
# 在打开的小程序中找到并点击'我的照片查询'
my_photos_location = pyautogui.locateOnScreen(r'C:\Users\Administrator\Desktop\我的照片查询.png')
if my_photos_location:
my_photos_x, my_photos_y = pyautogui.center(my_photos_location)
pyautogui.click(my_photos_x, my_photos_y)
else:
print('未找到'我的照片查询'')
# 等待页面加载
time.sleep(5)
# 在打开的页面中找到并点击'考试照片查询'
exam_photos_location = pyautogui.locateOnScreen(r'C:\Users\Administrator\Desktop\考试照片查询.png')
if exam_photos_location:
exam_photos_x, exam_photos_y = pyautogui.center(exam_photos_location)
pyautogui.click(exam_photos_x, exam_photos_y)
else:
print('未找到'考试照片查询'')
错误解决
常见错误: OSError: Failed to read ... because file is missing, has improper permissions, or is an unsupported or invalid format
这个错误是由于无法找到指定的图片文件导致的。请确保你提供的图片文件路径是正确的,并且文件存在于指定路径中。另外,还要确保你的程序有足够的权限来读取该文件。
代码说明
- 使用
subprocess.Popen()打开桌面小程序。 - 使用
pyautogui.locateOnScreen()查找目标按钮的图片。 - 使用
pyautogui.center()获取按钮的中心坐标。 - 使用
pyautogui.click()点击按钮。
注意
- 请将代码中的
program_path和图片文件的路径修改为正确的路径。 - 确保图片文件格式正确,例如 PNG 或 JPG。
- 如果你在使用
pyautogui时遇到问题,请参考官方文档:https://pyautogui.readthedocs.io/。
希望本文对你有所帮助。如有任何问题,请随时留言。
原文地址: https://www.cveoy.top/t/topic/ftPr 著作权归作者所有。请勿转载和采集!