What Is a Browser Context? β Definition and How It Works
Learn what a browser context isolates, why it is cheaper than a separate browser, and how to use contexts for parallel collection.
A browser context is an isolated state container inside a single browser process. It has its own cookies, storage, cache, and permissions, and cannot see any other context's.
1. What It Isolates
| Isolated per context | Shared across contexts |
|---|---|
| Cookies | The browser process |
| Local and session storage | Rendering engine and memory pool |
| IndexedDB | Installed browser version |
| Cache | The machine's network stack |
| Permissions | β |
| Service workers | β |
The practical summary: everything that constitutes a session is isolated; the expensive process is shared.
2. Why That Matters for Cost
Launching a browser is expensive in time and memory. Creating a context inside a running browser is cheap.
Running twenty independent sessions therefore means one browser and twenty contexts, not twenty browsers β a substantial difference at any concurrency.
3. The Standard Pattern
browser = playwright.chromium.launch()
for task in tasks:
context = browser.new_context(locale="en-US", viewport={"width": 1366, "height": 900})
page = context.new_page()
try:
collect(page, task)
finally:
context.close()
browser.close()
One context per task, closed when the task finishes. That gives isolation without the cost of process churn.
4. Configuring a Context
Contexts accept the settings that define a session's identity: viewport, locale, timezone, user agent, geolocation, permissions, and an HTTP proxy.
Setting locale and timezone together β and consistently with the proxy's region β matters, because a context claiming one locale while egressing from another is internally contradictory.
5. Contexts and Session State
State can be exported from a context and restored into a new one, which avoids repeating an expensive login for every task.
Two cautions: exported state is credential-equivalent and must be protected accordingly, and it expires β a restored session that silently no longer authenticates produces results that look like changed content.
6. Isolation Is a Correctness Requirement
Sharing one context across tasks leaks cookies and storage between them. That mixes identities, invalidates per-task assumptions, and makes failures irreproducible.
One task, one context is not an optimisation; it is what makes results attributable.
7. Close Them
Contexts hold memory and open resources. Long-running collectors that create contexts without closing them exhibit gradual memory growth that is easy to misattribute to the browser.
Closing in a finally block, as above, is the reliable pattern.
8. Summary
A browser context isolates cookies, storage, cache, and permissions inside a shared browser process, making many independent sessions cheap.
Use one context per task, configure locale and timezone consistently with the egress region, treat exported state as a credential, and always close contexts when the task ends.