Playwright._impl._api_types.error: target closed

playwright._impl._api_types.error: target closed

This error occurs when the target page or frame has been closed in Playwright.
When a page or frame is closed, it becomes invalid and any further actions or interactions with it will result in this error.

Here’s an example to demonstrate this error in Playwright:

    
const { chromium } = require('playwright');

(async () => {
  const browser = await chromium.launch();
  const page = await browser.newPage();

  // Navigate to a page or perform actions

  // Close the page before further interactions
  await page.close();

  // Attempting to interact with the closed page will result in the error
  try {
    await page.click('button'); // Throws "playwright._impl._api_types.error: target closed"
  } catch (error) {
    console.error(error);
  }

  await browser.close();
})();
    
  

In the above example, a new page is created and actions are performed on it. Afterward, the page is closed using the page.close() method.
Any further interactions with the closed page, such as trying to click a button, will throw the playwright._impl._api_types.error: target closed error.

Leave a comment