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?
- A single Chrome browser instance is launched
- 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?