【问题标题】:How can I handle the wait_for_selector function's TimeoutError in Playwright-python?如何在 Playwright-python 中处理 wait_for_selector 函数的 TimeoutError?
【发布时间】:2021-06-04 00:25:40
【问题描述】:

我想使用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")

我的代码有什么问题吗?

【问题讨论】:

    标签: exception playwright-python


    【解决方案1】:

    您必须从 playwright 导入 TimeoutError 才能捕获此异常:

    from playwright.async_api import async_playwright, TimeoutError
    

    【讨论】:

    • ... 如果您使用的是同步 API,请使用它:from playwright.sync_api import sync_playwright, TimeoutError
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-01-07
    • 2022-12-14
    • 2022-10-02
    • 2021-06-08
    • 1970-01-01
    • 2021-04-11
    • 2021-07-20
    相关资源
    最近更新 更多