User Agents

Web browsers and everyday web tech, explained

Configure Browser Automation Sessions: A Step-by-Step Guide

· 4 min read · Browsers

In this article
  1. What You'll Need
  2. Step-by-Step
  3. Common Mistakes
  4. Tips
  5. Troubleshooting
  6. Frequently Asked Questions

Browser automation sessions let you reuse a single logged-in browser state across multiple test runs or scraping tasks, saving time and avoiding repeated logins. This guide walks you through setting up persistent sessions using Playwright, Puppeteer, or Selenium, including how to save and load session data.

What You'll Need

  • A Node.js environment (for Playwright/Puppeteer) or Python 3 with pip (for Selenium)
  • One of the automation libraries installed: Playwright, Puppeteer, or Selenium WebDriver
  • A target website where you have valid login credentials (for testing session persistence)
  • A code editor and terminal

Step-by-Step

  1. Install the automation library. For Playwright: run npm init -y then npm install playwright. For Puppeteer: npm install puppeteer. For Selenium with Python: pip install selenium.
  2. Launch a browser instance with a persistent user data directory. This directory stores cookies, local storage, and other session data. In Playwright: const browser = await chromium.launchPersistentContext('./userdata', { headless: false });. In Puppeteer: const browser = await puppeteer.launch({ userDataDir: './userdata' });. In Selenium (Python): use Options and set user-data-dir via Chrome options.
  3. Navigate to your target site and log in manually. Open the browser window (non-headless) and perform the login as a normal user. This ensures the session cookies are saved to the user data directory.
  4. Close the browser cleanly. Ensure you close the browser context or driver properly so all session data is flushed to disk. In Playwright: await context.close();. In Puppeteer: await browser.close();. In Selenium: driver.quit();.
  5. Reuse the session in future runs. Launch the browser again with the same user data directory. The session should be restored, and you'll be logged in without needing to re-enter credentials.
  6. For non-persistent sessions, save/load cookies manually. If you can't use a user data directory, extract cookies after login and save them to a file. In Puppeteer: const cookies = await page.cookies(); then write to JSON. To restore: await page.setCookie(...cookies);. In Playwright: const storageState = await context.storageState(); then save to file; load with chromium.newContext({ storageState: 'state.json' });.
  7. Handle session expiry. Implement a check that detects if you're logged out (e.g., presence of a login button) and re-login automatically if needed.
  8. Use headless mode after verifying the session works. Once you confirm the session persists, you can switch to headless for production runs, but keep the same user data directory.

Common Mistakes

  • Using a fresh user data directory for every run, which defeats the purpose.
  • Forgetting to close the browser gracefully, causing incomplete writes to the session file.
  • Running headless during the initial login – some sites block headless browsers or require CAPTCHA.
  • Hardcoding credentials in the script instead of letting the session handle authentication.
  • Not accounting for session timeouts on the server side – your saved session may expire after hours or days.

Tips

  • Use a dedicated user data directory per site or project to avoid cross-site contamination.
  • For Selenium, prefer using user-data-dir over manually handling cookies, as it's more reliable.
  • Store session files in a gitignored folder to avoid accidentally committing sensitive login data.
  • If your site uses two-factor authentication, you may need to handle it once manually and then rely on the persistent session for subsequent runs.
  • Test session restoration in a separate script before integrating it into your main automation flow.

Troubleshooting

Session not persisting? Check that you're using the same user data directory path every time. Also ensure the browser process is fully closed before launching again – a lingering process can lock the directory.

Login state lost after a few minutes? The site may have a short session timeout. Increase the timeout by staying active or implement a re-login mechanism.

Headless mode fails to load the session? Some sites use browser fingerprinting that differs between headless and headed modes. Try using the same headless setting as when you saved the session, or use a stealth plugin.

Cookies file is empty? Make sure you're extracting cookies after the login is complete and the page has fully loaded. Also, some cookies are HttpOnly – they are still saved but not accessible via page.cookies() in Puppeteer; use the user data directory approach instead.

Browser crashes on launch with the user data directory? Delete the directory and recreate it. Corrupted session files can cause issues.

Frequently Asked Questions

Can I use the same session for multiple concurrent browser instances?

No, a user data directory is locked by the first browser process. For parallel runs, you need separate directories or use a cookie-based approach with different contexts.

Do I need to log in every time I change the user agent or viewport?

Not necessarily, but some sites invalidate sessions if the browser fingerprint changes. Test to see if your target site is strict about this.

Is it safe to commit session files to version control?

No, session files contain sensitive authentication tokens. Always add them to .gitignore.

How long does a saved session last?

It depends on the website's server-side session timeout. Some sites keep sessions alive for weeks, others for minutes. You can't control this from the client side.

Can I automate the login process instead of doing it manually?

Yes, but it's riskier and may violate the site's terms of service. Manual login once is safer and more reliable, especially with CAPTCHAs.

Does this work for mobile emulation?

Yes, you can save a session with a mobile viewport and reuse it, as long as you use the same device settings in subsequent runs.