I’m using Playwright on Windows with Node.js v14 and trying to keep browser state (cookies, local storage) across sessions using launchPersistentContext. Each run either creates a fresh session or throws errors related to state. I want the browser to persist user data, but the method doesn’t behave as expected.
The main issue usually comes from the userDataDir path or permissions. launchPersistentContext needs a valid directory to store cookies, local storage, and other user data. If the directory doesn’t exist or isn’t writable, persistence will fail.
Example working code:
const { chromium } = require('playwright');
(async () => {
const browser = await chromium.launchPersistentContext('/path/to/user/data', {
headless: false
});
const page = await browser.newPage();
await page.goto('https://example.com');
})();
Make sure the path is absolute, exists, and Node.js has permission to read/write there. This is a common cause of failures.
Sometimes the issue is due to version mismatches. If Playwright or the browser is not compatible, launchPersistentContext can behave unpredictably. Also, network policies or security settings may interfere with session storage.
Example using WebKit:
const { webkit } = require('playwright');
(async () => {
const browserContext = await webkit.launchPersistentContext('/my/user/data', {
viewport: { width: 1280, height: 720 },
ignoreHTTPSErrors: true
});
const page = await browserContext.newPage();
await page.goto('https://yoursite.com');
})();
Adding options like viewport or ignoreHTTPSErrors can stabilize behavior. Test in isolated environments and check logs for issues.