Page.goto: navigation failed because page was closed!

page.goto: navigation failed because page was closed!

This error occurs when attempting to navigate to a URL using the page.goto method but the page has been closed.
Let’s break it down and provide an example to understand it better.

Explanation

In Puppeteer, page.goto is used to navigate to a specific URL. The method opens a new browser page (tab), loads the requested URL, and waits for the navigation to complete. However, if the page has been closed prior to calling page.goto, this error occurs.

Example

const puppeteer = require('puppeteer');

async function navigateToURL(url) {
  const browser = await puppeteer.launch();
  const page = await browser.newPage();

  // Some other tasks or operations

  // Closing the browser page
  await page.close();

  // Attempting to navigate after page has been closed
  try {
    await page.goto(url);
  } catch (error) {
    console.error('Error:', error);
  }

  await browser.close();
}

navigateToURL('https://example.com');

In the example above, we create a new browser using puppeteer.launch and a new page using browser.newPage. Then, for demonstration purposes, we close the page using page.close before calling page.goto. This will result in the aforementioned error, as we are attempting navigation after closing the page.

Leave a comment