I’m having trouble with Puppeteer. When I try to add cookies to the default browser window or context, it opens up an extra window. This is weird because I only want one window.
Here’s what’s happening:
- I launch Puppeteer
- It opens two windows instead of one
- The first window has just one tab with about:blank
- The second window starts with about:blank but then goes to the URL I set
I’ve tried using page.setCookies
right after puppeteer.launch
, but that doesn’t add cookies to the whole context. I want all pages to share the same cookies.
My setup:
- Ubuntu 22.04
- Google Chrome for Testing 131.0.6778.87
I’m using puppeteer-extra with the StealthPlugin. Here’s a simplified version of my code:
const puppeteer = require('puppeteer-extra')
const StealthPlugin = require('puppeteer-extra-plugin-stealth')
puppeteer.use(StealthPlugin())
puppeteer.launch({ headless: false }).then(async browser => {
const context = await browser.createBrowserContext()
await context.setCookie(...cookies)
let page = await context.newPage()
await page.setUserAgent(userAgent)
await page.goto(url)
})
Any ideas on how to fix this? Thanks!
hey luke, i’ve seen this happen b4. try using browser.newPage()
instead of context.newPage()
. it might solve ur prob. also, check if ur stealth plugin is up to date. sometimes older versions can cause weird stuff like this. good luck!
I’ve encountered a similar issue with Puppeteer and extra windows. It seems the problem might be related to the StealthPlugin. Try launching the browser without it first to isolate the cause.
If that doesn’t work, consider using browser.pages()
to get all open pages and close the unwanted ones. Something like:
const pages = await browser.pages();
if (pages.length > 1) {
await pages[0].close();
}
This should close the extra window. Place this code right after browser.launch()
.
Another approach is to use defaultViewport: null
and args: ['--start-maximized']
in your launch options. This sometimes prevents the extra window from appearing.
If the issue persists, you might want to check if there are any conflicting extensions or settings in your Chrome profile that could be causing this behavior.
I’ve dealt with this exact issue before, and it can be frustrating. In my experience, the extra window is often caused by the StealthPlugin trying to set up a clean browsing environment. Here’s what worked for me:
-
Use browser.defaultBrowserContext()
instead of creating a new context. This way, you’re working with the default context that already exists.
-
Set your cookies on the browser instead of the context:
const defaultContext = browser.defaultBrowserContext();
await defaultContext.overridePermissions('https://example.com', ['cookies']);
const page = await browser.newPage();
await page.setCookie(...cookies);
- If you absolutely need a new context, try creating it before launching the browser:
const browser = await puppeteer.launch({ headless: false });
const context = await browser.createIncognitoBrowserContext();
const page = await context.newPage();
These steps helped me avoid the extra window issue while still maintaining the stealth features. Hope this helps!