9

I want to use Except TimeoutError to handle the timeout problem. But the script always throws me a TimeoutError, not print a message as I planed.

Here is my code:

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")

Is there anything wrong with my code?

2 Answers 2

6

You have to import TimeoutError from playwright to catch this exception:

from playwright.async_api import async_playwright, TimeoutError
Sign up to request clarification or add additional context in comments.

1 Comment

... and use this if you're using the sync API: from playwright.sync_api import sync_playwright, TimeoutError
0

It kept on throwing an error with the imported TimeoutError, so I left it out all together. Plus had to disable a flake8 error with # noqa

try:
  // some selector that might fail
  page.locator("whatever")
except: # noqa: E722
  print("Not today")
```

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.