Puppeteer creating multiple browser windows when setting up context with cookies

I’m having trouble with Puppeteer creating two browser windows when I try to set up cookies for a browser context. What I want is to apply cookies to the main browser window without creating additional windows.

The issue is that my code opens two separate browser windows. The first one just has a single tab showing about:blank. The second window also starts with about:blank but then navigates to my target URL.

I found that using page.setCookie right after puppeteer.launch works for a single window, but this approach doesn’t apply cookies to the whole context like I need.

Running on Ubuntu 22.04 with Chrome for Testing 131.0.6778.87.

const puppeteerExtra = require('puppeteer-extra')
const fileSystem = require('fs');
const cookieFile = "session-cookies.json";
const StealthMode = require('puppeteer-extra-plugin-stealth')
const { createPointer } = require("ghost-cursor")
puppeteerExtra.use(StealthMode())

// ... cookie loading code ...

sessionCookies = JSON.parse(sessionCookies)

let launchArgs = []
if(proxyServer) {
  launchArgs.push(`--proxy-server=${proxyServer}`)
}

puppeteerExtra.launch({ headless: false, args: launchArgs }).then(async browserInstance => {
  const newContext = await browserInstance.createBrowserContext()
  await newContext.setCookie(...sessionCookies)

  let newPage = await newContext.newPage()
  let customUserAgent = userAgent || defaultAgent
  console.log('Starting login with saved session..')
  await newPage.setUserAgent(customUserAgent)
  await newPage.setDefaultNavigationTimeout(120000)

  await newPage.goto(targetUrl)
  console.log('Session login complete')
})
response.send("browser started with session data");

Any suggestions on how to fix this dual window issue would be great.

You’re getting multiple windows because Puppeteer creates a default browser context when it launches, then your code creates another one. I’ve run into this before - easiest fix is just using the default context instead of making a new one. Grab the default context’s pages with browserInstance.pages() and set cookies on the first page: const pages = await browserInstance.pages(); await pages[0].setCookie(...sessionCookies). No extra contexts needed and your cookies still work across the whole session. Plus the default context handles cookie persistence way better than the ones you create manually.

use the default context instead of creating a new one. call browserInstance.defaultBrowserContext() - it’ll utilize the existing window instead of spawning another. fixed the same multiple windows issue for me.

This happens because Puppeteer opens a default browser window when you launch it, then you’re creating another browser context which opens a second window. I ran into this exact issue with cookie management in my automation scripts. Don’t create a new browser context after launch - either work with the default context or use the incognito option during launch. Try getting the default context first: const defaultContext = browserInstance.defaultBrowserContext() then set cookies on that context. You could also launch with { headless: false, args: [...launchArgs, '--new-window'] } but that probably won’t fix the real problem. Each browser context creates its own window in non-headless mode - that’s the key thing to understand.