playwright/docs/src/api/class-timeouterror.md
2021-08-23 09:21:53 -07:00

2.5 KiB

class: TimeoutError

  • extends: [Error]

TimeoutError is emitted whenever certain operations are terminated due to timeout, e.g. [method: Page.waitForSelector] or [method: BrowserType.launch].

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();
})();
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())
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()
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!");
      }
    }
  }
}
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!");
        }
    }
}