0

I am attempting to launch a Chrome browser in incognito mode with the code below. When I create a new page and navigate to a URL, I would like there to be one instance of the Chrome browser and I would like that single Chrome browser instance to be incognito.

However, the issue I'm running into is that even though I pass in the --incognito flag to launch the browser, the default browser context thinks it is not incognito. To try to work around this, I have tried the two workarounds, denoted in the code by "Workaround 1" and "Workaround 2". Both of these fail to accomplish the bolded sentence of the paragraph above.

import { Page, Browser } from "puppeteer";

const puppeteer = require('puppeteer');

async function run() {
    const browser: Browser = await puppeteer.launch({
        headless: false,
        defaultViewport: null,
        args: [ '--incognito' ], // Passing in the flag here
    });

    // Shockingly, the below logs FALSE!
    console.log(browser.defaultBrowserContext().isIncognito());

    // Workaround 1: This creates a new page in non-incognito mode, but I would like the new page to be in incognito mode.
    const browserPage: Page = await browser.newPage();
    browserPage.goto("https://www.google.com");

    // Workaround 2: This creates a new page in incognito mode, but I would like only one instance of the Chrome browser instead of two.
    const context = await browser.createIncognitoBrowserContext();
    const newContextPage: Page = await context.newPage();
    newContextPage.goto("https://www.amazon.com");
}

run();

Therefore, how can I accomplish these 2 items simultaneously?

  1. A single Chrome browser instance is launched
  2. That single Chrome browser instance is incognito

I think the answer is to set the default browser context to incognito, but how do I do that please?

aBlaze
  • 2,436
  • 2
  • 31
  • 63

3 Answers3

2

UPDATED:

You can try something like this, it will launch one chromium instance in incognito mode.

async function run() {
  const browser: Browser = await puppeteer.launch({
    headless: false,
    defaultViewport: null,
    args: [ '--incognito' ], // Passing in the flag here
  });

  const [ browserPage ] = await browser.pages();
  await browserPage.goto("https://www.google.com");
}

run();

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();

or

const browser = await puppeteer.launch();
const context = await browser.createIncognitoBrowserContext();
const page = await context.newPage();
Yevhen Laichenkov
  • 7,746
  • 2
  • 27
  • 33
1

according to this issue it is not possible.

domlas
  • 173
  • 7
0

Alternatively, as said here: Puppeteer Launch Incognito

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();
Arthur
  • 110
  • 3
  • 13