How do I handle cookies and sessions in Playwright?

In Playwright, cookies and sessions can be handled using the context.cookies() and context.addCookies() methods. Here's how to do it:

Getting Cookies

Python:

from playwright.sync_api import sync_playwright

with sync_playwright() as p:
    browser = p.chromium.launch()
    context = browser.new_context()
    page = context.new_page()
    page.goto("https://example.com")
    # Get cookies
    cookies = context.cookies()
    print(cookies)
    browser.close()

JavaScript:

const playwright = require('playwright');

(async () => {
  const browser = await playwright.chromium.launch();
  const context = await browser.newContext();
  const page = await context.newPage();
  await page.goto('https://example.com');
  // Get cookies
  const cookies = await context.cookies();
  console.log(cookies);
  await browser.close();
})();

Setting Cookies

Python:

from playwright.sync_api import sync_playwright

with sync_playwright() as p:
    browser = p.chromium.launch()
    context = browser.new_context()
    page = context.new_page()
    page.goto("https://example.com")
    # Set cookies
    context.add_cookies([
        {"name": "cookieName", "value": "cookieValue", "domain": "example.com", "path": "/", "expires": int(time.time()) + 1000}
    ])
    browser.close()

JavaScript:

const playwright = require('playwright');

(async () => {
  const browser = await playwright.chromium.launch();
  const context = await browser.newContext();
  const page = await context.newPage();
  await page.goto('https://example.com');
  // Set cookies
  await context.addCookies([
    {name: 'cookieName', value: 'cookieValue', domain: 'example.com', path: '/', expires: Date.now() + 1000}
  ]);
  await browser.close();
})();

Handling Sessions

Sessions in Playwright are handled by contexts. A new context starts a new session, and closing a context ends the session. Here's how to start and end a session:

Python:

from playwright.sync_api import sync_playwright

with sync_playwright() as p:
    browser = p.chromium.launch()
    context = browser.new_context()
    # New session started
    page = context.new_page()
    page.goto("https://example.com")
    # Session ends
    context.close()
    browser.close()

JavaScript:

const playwright = require('playwright');

(async () => {
  const browser = await playwright.chromium.launch();
  const context = await browser.newContext();
  // New session started
  const page = await context.newPage();
  await page.goto('https://example.com');
  // Session ends
  context.close();
  await browser.close();
})();

Note: Each new context in Playwright is isolated from others, meaning that they do not share cookies, localStorage, indexedDB, etc. If you want to preserve state across different contexts (i.e., keep the same session), you need to use contexts' storage state functionality.

Related Questions

Get Started Now

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