selenium.webdriver.common.api_request_context

APIRequestContext for making HTTP requests with browser cookie synchronization.

Classes

APIRequestContext(driver[, base_url, ...])

Makes HTTP requests with automatic browser cookie synchronization.

APIResponse(status, status_text, headers, ...)

Represents an HTTP response from an API request.

Exceptions

APIRequestFailure(response)

Raised when an API request returns a non-2xx status and fail_on_status_code is True.

exception selenium.webdriver.common.api_request_context.APIRequestFailure(response: APIResponse)[source]

Raised when an API request returns a non-2xx status and fail_on_status_code is True.

Attributes:

response: The APIResponse that triggered the failure.

args
with_traceback()

Exception.with_traceback(tb) – set self.__traceback__ to tb and return self.

class selenium.webdriver.common.api_request_context.APIResponse(status: int, status_text: str, headers: dict[str, str], url: str, body: bytes)[source]

Represents an HTTP response from an API request.

Attributes:

status: HTTP status code. status_text: HTTP status text. headers: Response headers as a dict. url: The request URL.

property ok: bool

Whether the response status is in the 200-299 range.

json() Any[source]

Parse the response body as JSON.

Returns:

The parsed JSON object.

text() str[source]

Decode the response body as UTF-8 text.

Returns:

The response body as a string.

body() bytes[source]

Return the raw response body bytes.

Returns:

The response body as bytes.

dispose() None[source]

Free the response body memory.

class selenium.webdriver.common.api_request_context.APIRequestContext(driver: WebDriver, base_url: str = '', extra_headers: dict[str, str] | None = None, timeout: float = 30.0, max_redirects: int = 10, fail_on_status_code: bool = False)[source]

Makes HTTP requests with automatic browser cookie synchronization.

Cookies from the browser session are sent with API requests, and cookies from API responses are synced back to the browser.

Args:

driver: The WebDriver instance to sync cookies with. base_url: Optional base URL prepended to relative request paths. extra_headers: Optional headers included in every request. timeout: Default request timeout in seconds. max_redirects: Maximum number of redirects to follow. fail_on_status_code: If True, raise APIRequestFailure for non-2xx responses.

new_context(base_url: str = '', extra_headers: dict[str, str] | None = None, storage_state: dict | str | Path | None = None, fail_on_status_code: bool = False) _IsolatedAPIRequestContext[source]

Create an isolated API request context that does not sync with the browser.

Args:

base_url: Optional base URL for this context. extra_headers: Optional headers for this context. storage_state: Optional cookies to pre-load, as a dict, JSON file path, or Path. fail_on_status_code: If True, raise APIRequestFailure for non-2xx responses.

Returns:

An _IsolatedAPIRequestContext instance.

get_storage_state(path: str | Path | None = None) dict[str, Any][source]

Export the current browser cookies as a storage state dict.

Args:

path: Optional file path to save the storage state as JSON.

Returns:

A dict with a “cookies” key containing the browser cookies.

delete(url: str, **kwargs: Any) APIResponse

Send a DELETE request.

Args:

url: The request URL (absolute or relative to base_url). **kwargs: Optional arguments: headers, params, data, form,

json_data, timeout, max_redirects, fail_on_status_code.

Returns:

An APIResponse object.

dispose() None

Close the underlying connection pool.

fetch(url: str, method: str = 'GET', **kwargs: Any) APIResponse

Send an HTTP request with a custom method.

Args:

url: The request URL (absolute or relative to base_url). method: The HTTP method to use. **kwargs: Optional arguments: headers, params, data, form,

json_data, timeout, max_redirects, fail_on_status_code.

Returns:

An APIResponse object.

get(url: str, **kwargs: Any) APIResponse

Send a GET request.

Args:

url: The request URL (absolute or relative to base_url). **kwargs: Optional arguments: headers, params, timeout, max_redirects, fail_on_status_code.

Returns:

An APIResponse object.

head(url: str, **kwargs: Any) APIResponse

Send a HEAD request.

Args:

url: The request URL (absolute or relative to base_url). **kwargs: Optional arguments: headers, params, timeout,

max_redirects, fail_on_status_code.

Returns:

An APIResponse object.

patch(url: str, **kwargs: Any) APIResponse

Send a PATCH request.

Args:

url: The request URL (absolute or relative to base_url). **kwargs: Optional arguments: headers, params, data, form,

json_data, timeout, max_redirects, fail_on_status_code.

Returns:

An APIResponse object.

post(url: str, **kwargs: Any) APIResponse

Send a POST request.

Args:

url: The request URL (absolute or relative to base_url). **kwargs: Optional arguments: headers, params, data, form,

json_data, timeout, max_redirects, fail_on_status_code.

Returns:

An APIResponse object.

put(url: str, **kwargs: Any) APIResponse

Send a PUT request.

Args:

url: The request URL (absolute or relative to base_url). **kwargs: Optional arguments: headers, params, data, form,

json_data, timeout, max_redirects, fail_on_status_code.

Returns:

An APIResponse object.