0.9.54版本的pyautogui查找一张不存在的图片时,会出现一个异常
File "C:\Users\rkey\AppData\Local\Programs\Python\Python38\lib\site-packages\pyautogui\__init__.py", line 174, in wrapper
raise ImageNotFoundException # Raise PyAutoGUI's ImageNotFoundException.
pyautogui.ImageNotFoundException
导致无法判断结果是None.
比如这样会出问题:
img_path= r".\\image_for_identify\\free_finished.jpg"
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)
于是我异常捕获了一下,发现提示”该异常未找到 ”
我反复在想这个异常,突然发现这个pyautogui.ImageNotFoundException
说不定就是一个处理异常的包呢,
再来一下异常处理,专门处理这个异常,发现能找到这个异常了
导入一下试试
from pyautogui import ImageNotFoundException
try:
location = pyautogui.locateOnScreen(图片路径, grayscale=True, confidence=0.8)
if location is None:
# 如果location是None,则抛出ImageNotFoundException
raise ImageNotFoundException('无法在屏幕上找到指定的图像。')
else:
# 如果找到了图像,执行一些操作
print(f"找到了图像,位置是:{location}")
except ImageNotFoundException as e:
# 捕获ImageNotFoundException异常
print(e)
ok,这个异常能捕获了
发表回复