7

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?

Tomas Vancoillie
  • 3,463
  • 2
  • 29
  • 45
Kaol
  • 431
  • 2
  • 7
  • 16

2 Answers2

5

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

from playwright.async_api import async_playwright, TimeoutError
Dmitry
  • 898
  • 10
  • 16
  • 3
    ... and use this if you're using the sync API: `from playwright.sync_api import sync_playwright, TimeoutError` – caram Jul 01 '21 at 09:10
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")
```
Tomas Vancoillie
  • 3,463
  • 2
  • 29
  • 45