How to create standalone incognito session with Puppeteer?

Hey everyone! I’m working on a project using Puppeteer and I’m trying to figure out how to launch a browser in incognito mode. Right now, I’m connecting to a browser using a WebSocket endpoint, but I want the whole session to be incognito from the start.

I’ve tried using createIncognitoBrowserContext(), but it seems like this just creates an incognito tab within the main browser session. What I’m after is a completely separate incognito browser instance.

I’ve also seen that you can pass --incognito as an argument when launching the browser, like this:

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

Is this the right way to go about it? Or is there a better method I’m missing?

Any help or tips would be awesome. Thanks in advance!

I’ve had success launching a standalone incognito session with Puppeteer by using the --incognito flag, as you mentioned. However, I found that combining it with a few other arguments gives more reliable results:

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

The --no-first-run and --no-default-browser-check flags help prevent any unwanted dialogs or settings from interfering with your incognito session. Additionally, make sure you’re closing the browser properly after use to avoid any lingering processes. This approach has worked well for me in projects requiring a fresh, isolated browsing environment for each run.

I’ve dealt with this issue before, and here’s what worked for me. Instead of using --incognito, I found that creating a new user data directory for each session gives you more control and isolation:

const userDataDir = path.join(os.tmpdir(), 'puppeteer_user_data_' + Date.now());
const browser = await puppeteer.launch({
  userDataDir: userDataDir,
  args: ['--no-sandbox', '--disable-setuid-sandbox']
});

This approach creates a fresh profile every time, effectively giving you an incognito-like experience. Remember to clean up the temporary directories after you’re done to avoid cluttering your system. It’s a bit more work, but it gives you full control over the browser environment and ensures complete separation between sessions.

hey bob, ive used puppeteer for similar stuff. try this:

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

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

this gives u a separate incognito instance. hope it helps!