2021-01-07 11:46:05 -08:00
|
|
|
# class: TimeoutError
|
2022-07-05 16:24:50 -08:00
|
|
|
* since: v1.8
|
2021-01-07 11:46:05 -08:00
|
|
|
* extends: [Error]
|
|
|
|
|
2022-12-20 16:19:07 +01:00
|
|
|
TimeoutError is emitted whenever certain operations are terminated due to timeout, e.g. [`method: Locator.waitFor`] or [`method: BrowserType.launch`].
|
2021-08-23 13:57:38 +02:00
|
|
|
|
|
|
|
```js
|
|
|
|
const playwright = require('playwright');
|
|
|
|
|
|
|
|
(async () => {
|
|
|
|
const browser = await playwright.chromium.launch();
|
|
|
|
const context = await browser.newContext();
|
|
|
|
const page = await context.newPage();
|
|
|
|
try {
|
2023-06-27 11:53:53 +02:00
|
|
|
await page.locator('text=Foo').click({
|
2021-08-23 13:57:38 +02:00
|
|
|
timeout: 100,
|
2023-06-27 11:53:53 +02:00
|
|
|
});
|
2021-08-23 13:57:38 +02:00
|
|
|
} catch (error) {
|
|
|
|
if (error instanceof playwright.errors.TimeoutError)
|
2023-06-27 11:53:53 +02:00
|
|
|
console.log('Timeout!');
|
2021-08-23 13:57:38 +02:00
|
|
|
}
|
|
|
|
await browser.close();
|
|
|
|
})();
|
|
|
|
```
|
|
|
|
|
|
|
|
```python async
|
|
|
|
import asyncio
|
2023-09-13 15:18:15 +02:00
|
|
|
from playwright.async_api import async_playwright, TimeoutError as PlaywrightTimeoutError, Playwright
|
2021-08-23 13:57:38 +02:00
|
|
|
|
2023-09-13 15:18:15 +02:00
|
|
|
async def run(playwright: Playwright):
|
2021-08-23 13:57:38 +02:00
|
|
|
browser = await playwright.chromium.launch()
|
|
|
|
page = await browser.new_page()
|
|
|
|
try:
|
2022-07-13 11:50:18 +02:00
|
|
|
await page.locator("text=Example").click(timeout=100)
|
2021-08-23 13:57:38 +02:00
|
|
|
except PlaywrightTimeoutError:
|
|
|
|
print("Timeout!")
|
|
|
|
await browser.close()
|
|
|
|
|
|
|
|
async def main():
|
|
|
|
async with async_playwright() as playwright:
|
|
|
|
await run(playwright)
|
|
|
|
|
|
|
|
asyncio.run(main())
|
|
|
|
```
|
|
|
|
|
2021-08-23 09:21:53 -07:00
|
|
|
```python sync
|
|
|
|
from playwright.sync_api import sync_playwright, TimeoutError as PlaywrightTimeoutError
|
|
|
|
|
|
|
|
with sync_playwright() as p:
|
|
|
|
browser = p.chromium.launch()
|
|
|
|
page = browser.new_page()
|
|
|
|
try:
|
2022-07-13 11:50:18 +02:00
|
|
|
page.locator("text=Example").click(timeout=100)
|
2021-08-23 09:21:53 -07:00
|
|
|
except PlaywrightTimeoutError:
|
|
|
|
print("Timeout!")
|
|
|
|
browser.close()
|
|
|
|
```
|
|
|
|
|
2021-08-23 13:57:38 +02:00
|
|
|
```java
|
|
|
|
package org.example;
|
|
|
|
|
|
|
|
import com.microsoft.playwright.*;
|
|
|
|
|
|
|
|
public class TimeoutErrorExample {
|
|
|
|
public static void main(String[] args) {
|
|
|
|
try (Playwright playwright = Playwright.create()) {
|
|
|
|
Browser browser = playwright.firefox().launch();
|
|
|
|
BrowserContext context = browser.newContext();
|
|
|
|
Page page = context.newPage();
|
|
|
|
try {
|
2022-07-13 11:50:18 +02:00
|
|
|
page.locator("text=Example").click(new Locator.ClickOptions().setTimeout(100));
|
2021-08-23 13:57:38 +02:00
|
|
|
} catch (TimeoutError e) {
|
|
|
|
System.out.println("Timeout!");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
```
|
|
|
|
|
|
|
|
```csharp
|
|
|
|
using Microsoft.Playwright;
|
|
|
|
|
2022-04-19 21:23:26 +03:00
|
|
|
using var playwright = await Playwright.CreateAsync();
|
|
|
|
await using var browser = await playwright.Chromium.LaunchAsync();
|
|
|
|
var page = await browser.NewPageAsync();
|
|
|
|
try
|
2021-08-23 13:57:38 +02:00
|
|
|
{
|
2022-04-19 21:23:26 +03:00
|
|
|
await page.ClickAsync("text=Example", new() { Timeout = 100 });
|
|
|
|
}
|
|
|
|
catch (TimeoutException)
|
|
|
|
{
|
|
|
|
Console.WriteLine("Timeout!");
|
2021-08-23 13:57:38 +02:00
|
|
|
}
|
|
|
|
```
|