Examples
A browser context is the thing that lets you run multiple independent sessions in one browser. That is useful when you need one logged-in session per account, or when you want to avoid cookie leakage between jobs.
from playwright.sync_api import sync_playwright
with sync_playwright() as p:
browser = p.chromium.launch(headless=True)
admin_context = browser.new_context()
user_context = browser.new_context()
admin_page = admin_context.new_page()
user_page = user_context.new_page()
admin_page.goto("https://example.com/login")
user_page.goto("https://example.com/login")
# These sessions are isolated: cookies, localStorage, sessionStorage
print(admin_context.cookies())
print(user_context.cookies())
admin_context.close()
user_context.close()
browser.close()
In scraping, this is a practical middle ground: - one browser per request is expensive - one shared session for everything is fragile - multiple contexts in one browser is often the sane option
Practical tips
- Use a new context per job when session isolation matters: logged-in scraping, account rotation, checkout flows, geo-specific sessions.
- Reuse the browser process, not the context, when you want better throughput without mixing state.
- Close contexts aggressively. If you leave them hanging around, memory usage creeps up and the box gets weird after a while.
- Don’t treat context isolation as full fingerprint isolation: browser-level traits are still shared unless your setup changes them.
- For JavaScript-heavy sites and SPAs, the context is what lets tools like Playwright run the app like a real browser session instead of pretending HTML is enough.
- If you need clean runs every time, create a fresh context instead of trying to manually clear cookies and storage.
context = browser.new_context(
user_agent="Mozilla/5.0 ...",
locale="en-US",
timezone_id="America/New_York"
)
page = context.new_page()
page.goto("https://example.com")
If you're using a browser-based scraping layer through an API, this isolation is often handled for you behind the scenes. That's the whole point: you want the rendered page, not another pile of browser lifecycle code to maintain.
Use cases
- Running multiple account sessions in parallel without cookie collisions
- Scraping single-page apps that need JavaScript execution and session state
- Testing different login states: anonymous, basic user, admin
- Keeping retries clean after a session gets flagged or poisoned
- Scaling browser automation cheaper than launching a full browser for every request
- Isolating region, language, or device-specific sessions with different context settings