You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
!!! info "Headless vs Headed: how contexts show up"
104
+
Browser contexts are isolated logical environments. In headed mode, the first page created inside a new context will usually open in a new OS window. In headless mode, no window is shown — the isolation remains purely logical (cookies, storage, cache and auth state are still separate per context). Prefer contexts in headless/CI pipelines for performance and clean isolation.
Copy file name to clipboardExpand all lines: public/docs/deep-dive/browser-domain.md
+79Lines changed: 79 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -327,6 +327,85 @@ Browser contexts are essential for several automation scenarios:
327
327
4.**Session Isolation**: Prevent cross-contamination between test scenarios
328
328
5.**Parallel Scraping**: Scrape multiple sites with different configurations
329
329
330
+
### Headless vs Headed: Windows and Best Practices
331
+
332
+
Browser contexts are a logical isolation layer. What you actually see is the page created inside a context:
333
+
334
+
- In headed mode (visible UI), creating the first page inside a new browser context will typically open a new OS window. The context is the isolated environment; the page is what renders in a tab or window.
335
+
- In headless mode (no visible UI), no windows appear. The isolation still exists logically in the background, keeping cookies, storage, cache and auth state fully separate per context.
336
+
337
+
Recommendations:
338
+
339
+
- Prefer using multiple contexts in headless environments (e.g., CI/CD) for cleaner isolation, faster startup, and lower resource usage compared to launching multiple browser processes.
340
+
- Use contexts to simulate multiple users or sessions in parallel without cross-contamination.
341
+
342
+
Why contexts are efficient:
343
+
344
+
- Creating a new browser context is significantly faster and lighter than starting a whole new browser instance. This makes test suites and scraping jobs more reliable and scalable.
345
+
346
+
### CDP Hierarchy and Context Window Semantics (Advanced)
347
+
348
+
To reason precisely about contexts, it's useful to map Pydoll concepts to CDP:
349
+
350
+
- Browser (process): single Chromium process running the DevTools endpoint.
351
+
- BrowserContext: isolated profile inside that process (cookies, storage, cache, permissions).
352
+
- Target/Page: an individual top-level page, popup, or background target that you control.
353
+
354
+
CDP and `browserContextId`:
355
+
356
+
- When creating a page via `Target.createTarget`, passing `browserContextId` tells the browser which isolated profile the new page should belong to. Without this ID, the target is created in the default context.
357
+
- The ID is essential for isolation — it binds the new target to the correct storage/auth/permission boundary.
358
+
359
+
Why the first page in a context opens a window (headed):
360
+
361
+
- In headed mode, a page needs a top-level native window to render. A freshly created context initially has no window associated with it — it exists only in memory.
362
+
- The first page created in that context implicitly materializes a window for that context. Subsequent pages can open as tabs within that window.
363
+
364
+
Implications for `new_window`/`newWindow` semantics:
365
+
366
+
- If you attempt to create a page with "tab-like" behavior (no new top-level window) in a context that has no existing window (first page), the browser may error because there is no host window to attach the tab to.
367
+
- Practically: treat the first page in a new context (headed) as requiring a top-level window. Afterwards, you can create additional pages as tabs.
368
+
369
+
Headless mode makes this distinction moot:
370
+
371
+
- With no visible UI, windows vs tabs are logical constructs only. Context isolation is enforced the same way, but nothing is rendered, so there is no requirement to bootstrap a native window for the first page.
372
+
373
+
### Context-specific Proxy: sanitize + auth via Fetch events
374
+
375
+
When creating a browser context with a private proxy (credentials embedded in the URL), Pydoll follows a two-step strategy to avoid leaking credentials and reliably authenticate:
376
+
377
+
1) Sanitize the proxy server in the CDP command
378
+
379
+
- If you pass `proxy_server='http://user:pass@host:port'`, only the credential-free URL is sent to CDP (`http://host:port`).
380
+
- Internally, Pydoll extracts and stores the credentials keyed by `browserContextId`.
381
+
382
+
2) Attach per-context auth handlers on first tab
383
+
384
+
- When you open a `Tab` inside that context, Pydoll enables Fetch events for that tab and registers two temporary listeners:
385
+
-`Fetch.requestPaused`: continues normal requests.
386
+
-`Fetch.authRequired`: automatically responds with the stored `user`/`pass`, then disables Fetch to avoid intercepting further requests.
387
+
388
+
Why this design?
389
+
390
+
- Prevents credential exposure in command logs and CDP parameters.
391
+
- Keeps the auth scope strictly limited to the context that requested the proxy.
392
+
- Works in both headed and headless modes (the auth flow is network-level, not UI-dependent).
0 commit comments