Puppeteer Launch Incognito

rpascal picture rpascal · Aug 29, 2018 · Viewed 11.8k times · Source

I am connected to a browser using a ws endpoint (puppeteer.connect({ browserWSEndpoint: '' })).

When I launch the browser that I ultimately connect to, is there a way to launch this in incognito?

I know I can do something like this:

const incognito = await this.browser.createIncognitoBrowserContext();

But it seems like the incognito session is tied to the originally opened browser. I just want it to be by itself.

I also see you can do this:

const baseOptions: LaunchOptions = { args: ['--incognito']};

But I am not sure if this is the best way or not.

Any advice would be appreciated. Thank you!

Answer

Grant Miller picture Grant Miller · Aug 29, 2018

The best way to accomplish your goal is to launch the browser directly into incognito mode by passing the --incognito flag to puppeteer.launch():

const browser = await puppeteer.launch({
  args: [
    '--incognito',
  ],
});

Alternatively, you can create a new incognito browser context after launching the browser using browser.createIncognitoBrowserContext():

const browser = await puppeteer.launch();
const context = await browser.createIncognitoBrowserContext();

You can check whether a browser context is incognito using browserContext.isIncognito():

if (context.isIncognito()) { /* ... */ }