首页 > 解决方案 > 如何在 Playwright-python 中处理 wait_for_selector 函数的 TimeoutError?

问题描述

我想用它Except TimeoutError来处理超时问题。但是脚本总是给我一个 TimeoutError,而不是按我的计划打印一条消息。

这是我的代码:

try:
    await page.wait_for_selector("#winiframe_main", timeout=10000, state='detached')
    print("The frame is detached.")
except TimeoutError:
    print("The frame is not detached")

我的代码有什么问题吗?

标签: exceptionplaywright-python

解决方案


您必须导入TimeoutErrorfromplaywright才能捕获此异常:

from playwright.async_api import async_playwright, TimeoutError

推荐阅读