How do I manage sessions and cookies effectively in Headless Chromium?

When working with headless Chromium, managing sessions and cookies is essential for tasks like web scraping, automated testing, or any scenario where you need to persist authentication state or preferences across multiple requests.

Puppeteer (JavaScript)

If you are using Puppeteer, which is a Node library that provides a high-level API to control headless Chrome, you can manage sessions and cookies using the following methods:

Set Cookies

To set cookies in Puppeteer, you can use the page.setCookie(...cookies) method:

const puppeteer = require('puppeteer');

(async () => {
  const browser = await puppeteer.launch({ headless: true });
  const page = await browser.newPage();

  // Set cookies
  await page.setCookie({
    'name': 'sessionid',
    'value': '123456',
    'domain': ''

  // Navigate to the page which reads the cookies
  await page.goto('');

  // Do something on the page...

  await browser.close();

Get Cookies

To get cookies, you can use page.cookies():

// Get cookies
const cookies = await page.cookies();

Clearing Cookies

To clear cookies, you can use page.deleteCookie(...cookies):

// Clear cookies
await page.deleteCookie({
  'name': 'sessionid',
  'domain': ''

Selenium with ChromeDriver (Python)

If you're using Selenium with ChromeDriver in Python, you can manage cookies with the WebDriver API.

Set Cookies

from selenium import webdriver

# Start a new browser session
browser = webdriver.Chrome(executable_path='path/to/chromedriver')

# Open a page before setting cookies

# Add a cookie
browser.add_cookie({'name': 'sessionid', 'value': '123456', 'domain': ''})

# Continue with your browsing activities

Get Cookies

# Get all cookies
cookies = browser.get_cookies()

# Get a specific cookie
cookie = browser.get_cookie('sessionid')

Clearing Cookies

# Delete a specific cookie

# Delete all cookies

# Close the browser session

Chrome DevTools Protocol (CDP)

For a low-level approach, you can directly use the Chrome DevTools Protocol (CDP) to manage cookies. This is useful if you're not using a high-level library like Puppeteer or Selenium.

Set Cookies

const CDP = require('chrome-remote-interface');

CDP(async (client) => {
  const { Network } = client;
  await Network.enable();

  // Set a cookie
  const cookieStatus = await Network.setCookie({
    name: 'sessionid',
    value: '123456',
    domain: ''

  if (cookieStatus.success) {
    console.log('Cookie set successfully');

  // Close the CDP session
}).on('error', (err) => {
  console.error('Cannot connect to browser:', err);

Get Cookies

// Get all cookies
const allCookies = await Network.getCookies();

// Get cookies for specific URLs
const cookiesForURL = await Network.getCookies(['']);

Clearing Cookies

// Clear cookies
await Network.clearBrowserCookies();

// Clear browser cache
await Network.clearBrowserCache();


Remember that managing cookies and sessions implies handling sensitive data. Always ensure you comply with privacy laws and website terms of service when using these techniques. Additionally, when running automated scripts, make sure your actions do not overload the website's servers.

For any approach, it’s crucial to understand the context of the website you are working with, the structure of cookies it uses, and how sessions are managed. Properly managing this information will allow you to maintain a persistent state across multiple pages or even different browsing sessions.

Related Questions

Get Started Now

WebScraping.AI provides rotating proxies, Chromium rendering and built-in HTML parser for web scraping