Puppeteer – Protocol error (Page.navigate): Target closed

What “Target closed” means

When you launch a browser via puppeteer.launch it will start a browser and connect to it. From there on any function you execute on your opened browser (like page.goto) will be send via the Chrome DevTools Protocol to the browser. A target means a tab in this context.

The Target closed exception is thrown when you are trying to run a function, but the target (tab) was already closed.

Similar error messages

The error message was recently changed to give more meaningful information. It now gives the following message:

Error: Protocol error (Target.activateTarget): Session closed. Most likely the page has been closed.


Why does it happen

There are multiple reasons why this could happen.

  • You used a resource that was already closed

    Most likely, you are seeing this message because you closed the tab/browser and are still trying to use the resource. To give an simple example:

    const browser = await puppeteer.launch();
    const page = await browser.newPage();
    
    await browser.close();
    await page.goto('http://www.google.com');
    

    In this case the browser was closed and after that, a page.goto was called resulting in the error message. Most of the time, it will not be that obvious. Maybe an error handler already closed the page during a cleanup task, while your script is still crawling.

  • The browser crashed or was unable to initialize

    I also experience this every few hundred requests. There is an issue about this on the puppeteer repository as well. It seems to be the case, when you are using a lot of memory or CPU power. Maybe you are spawning a lot of browser? In these cases the browser might crash or disconnect.

    I found no “silver bullet” solution to this problem. But you might want to check out the library puppeteer-cluster (disclaimer: I’m the author) which handles these kind of error cases and let’s you retry the URL when the error happens. It can also manage a pool of browser instances and would also simplify your code.

Leave a Comment