24

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!

Grant Miller
  • 27,532
  • 16
  • 147
  • 165
rpascal
  • 723
  • 1
  • 8
  • 25

3 Answers3

36

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()) { /* ... */ }
Grant Miller
  • 27,532
  • 16
  • 147
  • 165
  • 3
    When the browser is launched in `--incognito` mode, the `newPage()` still share the data and not incognito. Any suggestion? – Vikash Rathee Nov 25 '21 at 06:04
34

the solutions above didn't work for me:

an incognito window is created, but then when the new page is created, it is no longer incognito.

The solution that worked for me was:

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

then you can use page and it's an incognito page

Benni
  • 778
  • 8
  • 22
MrE
  • 19,584
  • 12
  • 87
  • 105
  • 2
    It's true. Run puppeteer without headless and see for yourselves. Pages are not incognito in the other answers – Neal Bozeman Feb 06 '20 at 16:05
  • It worked here! Although the incognito window was not black, which was causing me confusion at first sigh, but it was indeed the chromium incognito window – Victor May 16 '21 at 04:38
-1

For Puppeteer sharp it's rather messy but this seems to work.. Hopefully it helps someone.

using (Browser browser = await Puppeteer.LaunchAsync(options))
{
     // create the async context 
    var context = await browser.CreateIncognitoBrowserContextAsync();

    // get the page created by default when launch async ran and close it whilst keeping the browser active
    var browserPages = await browser.PagesAsync();
    await browserPages[0].CloseAsync();

    // create a new page using the incognito context
    using (Page page = await context.NewPageAsync())
    {
        // do something 
    }
}
atoms
  • 2,993
  • 2
  • 22
  • 43