puppeteer: [Bug]: Protocol error: Connection closed.

Minimal, reproducible example


class PuppeteerBrowser {
constructor(options = PuppeteerBrowser.defaultOptions) {
this.browser = null;
this.options = options;
}

static get defaultOptions() {
return {};
}

async init() {
if (this.browser) return;

const launchOptions = {
  protocolTimeout: 0,
  executablePath: process.env.CHROME_EXECUTABLE, // Optional: Define if you have a custom executable path
  ignoreHTTPSErrors: true,
  defaultViewport: {
    width: 1920,
    height: 1080,
  },
  args: [
    "--window-size=1920,1080",
    "--disable-gpu",
    "--disable-features=IsolateOrigins,site-per-process",
    "--disable-blink-features=AutomationControlled",
    "--blink-settings=imagesEnabled=true",
    "--enable-features=NetworkService",
    "--disable-dev-shm-usage",
    "--no-sandbox",
    "--disable-setuid-sandbox",
  ],
};

if (process.env.DISABLE_HEADLESS !== "true") {
  launchOptions.headless = true;
  launchOptions.args.push("--headless=new");
} else {
  launchOptions.headless = false;
}

this.browser = await puppeteer.launch(launchOptions);
this.guid = Math.floor(Math.random() * 1000000000);
}

async context() {
await this.init();
return await this.browser.createIncognitoBrowserContext();
}

async close() {
if (!this.browser) return;
await this.browser.close();
this.browser = null;
}
}

module.exports = PuppeteerBrowser;
`

Background

i have been trying to trying to scrape the web site

Expectation

it should successfully work

Reality

Lot of Protocol error: Connection closed. error

Puppeteer configuration file (if used)

No response

Puppeteer version

22.14.0

Node version

20

Package manager

npm

Package manager version

10.8.0

Operating system

Linux

About this issue

  • Original URL
  • State: closed
  • Created 2 years ago
  • Comments: 25

Most upvoted comments

You generally should not call browser.close() on ‘disconnected’ since the connection is closed by this point and you cannot send the close command to the browser.