解决方案 | pyautogui实现等待特定图片(对话框)出现(可设置等待超时时间)
1、问题
为了等待某个【转换完毕】的图片(对话框)出现,如何设置?
2、解决方案代码如下
下面代码实现的是:设置超时时间为10s,当你在完成前序工作以后,代码开始进入等待,一旦你的特定图片出现了,马上探测到它的位置并且实现点击按钮。
(注意:如果等待时间超过了10s,那么就会返回提示:Timed out after 10 seconds.)
import pyautogui import time ## 执行某些工作 # 执行代码区域... ## 下面是等待完成的代码 img_path= r".\\image_for_identify\\free_finished.jpg" #存放特定图片的路径 imageFile =img_path location = None start_time = time.time() timeout = 10 # 设置超时时间 while location is None and time.time() - start_time < timeout: location = pyautogui.locateOnScreen(imageFile,grayscale=True, confidence=0.8) if location is None: time.sleep(0.1) if location is not None: left_pos,top_pos,hei_pos ,width_pos= location #"确定"按钮的中心坐标,根据你自己的图像进行计算 x_c=left_pos+0.8*width_pos y_c=top_pos+0.8*hei_pos pyautogui.click(x_c,y_c) else: print("Timed out after {} seconds.".format(timeout))
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· .NET10 - 预览版1新功能体验(一)