docs(errors): add example for catching Timeout errors (#8362)
This commit is contained in:
parent
25a4c7b3df
commit
953f19538a
|
|
@ -3,3 +3,100 @@
|
|||
|
||||
TimeoutError is emitted whenever certain operations are terminated due to timeout, e.g. [`method:
|
||||
Page.waitForSelector`] or [`method: BrowserType.launch`].
|
||||
|
||||
```js
|
||||
const playwright = require('playwright');
|
||||
|
||||
(async () => {
|
||||
const browser = await playwright.chromium.launch();
|
||||
const context = await browser.newContext();
|
||||
const page = await context.newPage();
|
||||
try {
|
||||
await page.click("text=Foo", {
|
||||
timeout: 100,
|
||||
})
|
||||
} catch (error) {
|
||||
if (error instanceof playwright.errors.TimeoutError)
|
||||
console.log("Timeout!")
|
||||
}
|
||||
await browser.close();
|
||||
})();
|
||||
```
|
||||
|
||||
```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:
|
||||
page.click("text=Example", timeout=100)
|
||||
except PlaywrightTimeoutError:
|
||||
print("Timeout!")
|
||||
browser.close()
|
||||
```
|
||||
|
||||
```python async
|
||||
import asyncio
|
||||
from playwright.async_api import async_playwright, TimeoutError as PlaywrightTimeoutError
|
||||
|
||||
async def run(playwright):
|
||||
browser = await playwright.chromium.launch()
|
||||
page = await browser.new_page()
|
||||
try:
|
||||
await page.click("text=Example", timeout=100)
|
||||
except PlaywrightTimeoutError:
|
||||
print("Timeout!")
|
||||
await browser.close()
|
||||
|
||||
async def main():
|
||||
async with async_playwright() as playwright:
|
||||
await run(playwright)
|
||||
|
||||
asyncio.run(main())
|
||||
```
|
||||
|
||||
```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 {
|
||||
page.click("text=Example", new Page.ClickOptions().setTimeout(100));
|
||||
} catch (TimeoutError e) {
|
||||
System.out.println("Timeout!");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
```csharp
|
||||
using System.Threading.Tasks;
|
||||
using Microsoft.Playwright;
|
||||
using System;
|
||||
|
||||
class Program
|
||||
{
|
||||
public static async Task Main()
|
||||
{
|
||||
using var playwright = await Playwright.CreateAsync();
|
||||
await using var browser = await playwright.Chromium.LaunchAsync();
|
||||
var page = await browser.NewPageAsync();
|
||||
try
|
||||
{
|
||||
await page.ClickAsync("text=Example", new() { Timeout = 100 });
|
||||
}
|
||||
catch (TimeoutException)
|
||||
{
|
||||
Console.WriteLine("Timeout!");
|
||||
}
|
||||
}
|
||||
}
|
||||
```
|
||||
|
|
|
|||
20
types/types.d.ts
vendored
20
types/types.d.ts
vendored
|
|
@ -8802,6 +8802,26 @@ export namespace errors {
|
|||
* TimeoutError is emitted whenever certain operations are terminated due to timeout, e.g.
|
||||
* [page.waitForSelector(selector[, options])](https://playwright.dev/docs/api/class-page#page-wait-for-selector) or
|
||||
* [browserType.launch([options])](https://playwright.dev/docs/api/class-browsertype#browser-type-launch).
|
||||
*
|
||||
* ```js
|
||||
* const playwright = require('playwright');
|
||||
*
|
||||
* (async () => {
|
||||
* const browser = await playwright.chromium.launch();
|
||||
* const context = await browser.newContext();
|
||||
* const page = await context.newPage();
|
||||
* try {
|
||||
* await page.click("text=Foo", {
|
||||
* timeout: 100,
|
||||
* })
|
||||
* } catch (error) {
|
||||
* if (error instanceof playwright.errors.TimeoutError)
|
||||
* console.log("Timeout!")
|
||||
* }
|
||||
* await browser.close();
|
||||
* })();
|
||||
* ```
|
||||
*
|
||||
*/
|
||||
class TimeoutError extends Error {}
|
||||
|
||||
|
|
|
|||
Loading…
Reference in a new issue