如何处理 Playwright-python 中 wait_for_selector 函数的 TimeoutError?

Kao*_*aol 7 python exception playwright-python

我想用它Except 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")
Run Code Online (Sandbox Code Playgroud)

我的代码有什么问题吗?

Dmi*_*nin 5

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

from playwright.async_api import async_playwright, TimeoutError
Run Code Online (Sandbox Code Playgroud)

  • ...如果您使用同步 API,请使用此:`from playwright.sync_api importsync_playwright, TimeoutError` (3认同)