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.