Session

A CDP session representing a single browser tab.

Usage

Source

Session()

Provides methods for navigation, viewport control, JavaScript evaluation, and DOM queries needed for screenshot capture.

Parameters

cdp: SyncCDP

The SyncCDP connection to use.

width: int = 992

Initial viewport width in pixels.

height: int = 744
Initial viewport height in pixels.

Methods

Name Description
close() Close this session (tab).
evaluate() Evaluate a JavaScript expression and return the result.
get_element_bounds() Get the bounding box of an element matched by a CSS selector.
get_elements_union_bounds() Get the union bounding box of all elements matched by the given selectors.
navigate() Navigate to a URL and wait for the page to load.
set_user_agent() Set a custom User-Agent string.
set_viewport() Set the viewport dimensions.

close()

Close this session (tab).

Usage

Source

close()

evaluate()

Evaluate a JavaScript expression and return the result.

Usage

Source

evaluate(expression)
Parameters
expression: str
JavaScript expression to evaluate.
Returns
Any
The result value from the JavaScript evaluation.

get_element_bounds()

Get the bounding box of an element matched by a CSS selector.

Usage

Source

get_element_bounds(selector)
Parameters
selector: str
CSS selector to match.
Returns
ClipRect
The bounding rectangle of the element.
Raises
ValueError
If the selector matches no elements.

get_elements_union_bounds()

Get the union bounding box of all elements matched by the given selectors.

Usage

Source

get_elements_union_bounds(selectors)
Parameters
selectors: list[str]
List of CSS selectors to match.
Returns
ClipRect
The union bounding rectangle encompassing all matched elements.


set_user_agent()

Set a custom User-Agent string.

Usage

Source

set_user_agent(user_agent)

set_viewport()

Set the viewport dimensions.

Usage

Source

set_viewport(width, height, device_scale_factor=1.0, mobile=False)
Parameters
width: int

Viewport width in pixels.

height: int

Viewport height in pixels.

device_scale_factor: float = 1.0

Device scale factor (e.g., 2 for retina).

mobile: bool = False
Whether to emulate a mobile device.