BrowserContext
conatus.utils.browser.context
¶
Browser context module.
A browser context is a container for a set of pages. A FullBrowser can have one or multiple
contexts. Think of a context as a container for cookies, etc. that are shared
across multiple pages. A browser can have an incognito context in addition to a
normal context, for example.
This module is used in conjunction with the Page module. Each context can have one or
multiple pages.
Configuration¶
You can configure some parameters for the context:
-
Screen size: Settings related to the screen size.
initial_viewport: The initial size of the viewport. (The viewport size can change during the browsing session, I believe.) Default is (1100, 700).device_scale_factor: The device scale factor. Default is 1.0.
-
Device settings: Settings related to the device.
device_type: The device type. Default isDesktop Chrome HiDPI.
Note
You have to pass a full BrowserConfig instance during
instantiation of the context, not just the context configuration.
Viewport
¶
DeviceConfig
¶
Bases: BaseModel
Device configuration.
Acts as a wrapper around what Playwright calls a device.
More information can be found in the Playwright documentation: https://playwright.dev/python/docs/api/class-playwright#playwright-devices
Source code in conatus/utils/browser/context.py
BrowserContext
¶
BrowserContext(
pw: Playwright,
browser_instance: Browser,
config: BrowserConfig,
*,
async_init: bool = False
)
Browser context module.
Init in sync or async context¶
You can initialize the browser context in a sync or async context:
from conatus.utils.browser.context import BrowserContext
from conatus.utils.browser.full import FullBrowser
browser = FullBrowser(headless=True)
pw, browser_instance, cfg = browser.pw, browser.instance, browser.config
# Sync context
context = BrowserContext(pw, browser_instance, cfg)
# Async context
# context = await BrowserContext.init_async(pw, browser_instance, cfg)
| PARAMETER | DESCRIPTION |
|---|---|
pw
|
The Playwright instance.
TYPE:
|
browser_instance
|
The Playwright browser instance.
TYPE:
|
config
|
The browser configuration.
TYPE:
|
async_init
|
Whether this function is called in an async context. Users should not set this parameter. Defaults to False.
TYPE:
|
Source code in conatus/utils/browser/context.py
device
instance-attribute
¶
device: DeviceConfig = _set_device(
pw,
initial_viewport=initial_viewport,
device_scale_factor=device_scale_f,
device_type=device_type,
)
The device configuration.
init_async
async
classmethod
¶
init_async(
pw: Playwright,
browser_instance: Browser,
config: BrowserConfig,
) -> BrowserContext
Create a BrowserContext instance in an async context.
| PARAMETER | DESCRIPTION |
|---|---|
pw
|
The Playwright instance.
TYPE:
|
browser_instance
|
The Playwright browser instance.
TYPE:
|
config
|
The browser configuration.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
BrowserContext
|
The browser context.
TYPE:
|
Source code in conatus/utils/browser/context.py
new_page_async
async
¶
new_page
¶
close_async
async
¶
close
¶
Close the browser context.
Warning
Please try to ensure that, if you use this method, you also remove the page from the context's list of pages.
from conatus.utils.browser.full import FullBrowser
browser = FullBrowser()
bad_context = browser.contexts[0]
bad_context.close()
browser.contexts.pop(0)