Chrome browser doesn't save cookies between sessions when using Puppeteer

I have a problem with my Puppeteer automation script. When I close the browser and start it again, all the cookies disappear. I need them to stay saved for my next session.

Here’s my current code setup:

const puppeteer = require('puppeteer');

(async () => {
    const profilePath = 'D:\\temp_folder\\abc';
    
    const launchOptions = [
        ...puppeteer.defaultArgs({ headless: false }).map(option => {
            if (option.startsWith('--disable-features')) {
                return option.replace('Translate,', '').replace(',Translate', '');
            }
            return option;
        }),
        '--window-size=1920,1080',
        '--disable-sync',
        '--no-sandbox',
        '--ignore-ssl-errors'
    ].filter(option => !['--enable-automation', '--disable-extensions'].includes(option));
    
    const chromeInstance = await puppeteer.launch({
        args: launchOptions,
        acceptInsecureCerts: true,
        headless: false,
        ignoreHTTPSErrors: true,
        defaultViewport: null,
        ignoreDefaultArgs: true,
        userDataDir: profilePath,
        pipe: true
    });
    
    const newPage = await chromeInstance.newPage();
    
    // Additional code here
    
})();

Any suggestions on how to fix this issue?

I encountered a similar issue a while back. The problem typically stems from how Puppeteer interacts with the Chrome instance. Ensure the userDataDir you’re using is valid and accessible. Additionally, try including a delay after closing the browser to allow Chrome ample time to save the cookies. It might also help to check any antivirus settings, as they can sometimes interfere with file operations. Running your script with elevated permissions could also resolve potential write permission issues.

This happened to me last week! Remove the pipe: true option from your launch config - it messes with session persistence sometimes. Also check you’re not running multiple instances at once since they’ll conflict over the same userDataDir. Fixed it for me once I dropped that pipe setting.

This sounds like Chrome’s session handling acting up when Puppeteer launches it. I’ve hit this exact issue before - try adding --disable-web-security and --disable-features=VizDisplayCompositor flags to help with cookie persistence. Make sure you’re closing the browser properly with await browser.close() instead of just killing the process. Chrome needs that proper shutdown to write cookies to disk. Also double-check that your userDataDir path exists and has write permissions - Windows Defender loves blocking access to random directories.