Skip to main content

BrowserContext

Struct BrowserContext 

Source
pub struct BrowserContext { /* private fields */ }
Expand description

A browser context. The default context has browser_context_id == None; isolated contexts are created via Browser::new_context.

Implementations§

Source§

impl BrowserContext

Source

pub fn browser_context_id(&self) -> Option<&str>

The CDP browser-context id, or None for the default context.

Source

pub async fn new_page(&self) -> Result<Page>

Open a new page in this context.

Source

pub async fn add_init_script(&self, script: &str) -> Result<()>

Add a script evaluated before each document load (applied to new pages).

Source

pub fn set_default_timeout(&self, timeout_ms: u64)

Set the default action timeout (ms) for pages in this context.

Source

pub async fn set_extra_http_headers(&self, headers: Headers) -> Result<()>

Set extra HTTP headers sent on every request (applied to new + existing pages).

Source

pub fn request(&self) -> APIRequestContext

Return a standalone HTTP client (APIRequestContext) tied to this context. Its default headers are seeded (best-effort) from the context’s extra_http_headers. The client is otherwise independent of the browser — it makes direct HTTP requests.

Source

pub fn pages(&self) -> Vec<Page>

All pages opened in this context.

Source

pub fn browser(&self) -> Browser

The owning browser.

Source

pub async fn add_cookies(&self, cookies: &[Cookie]) -> Result<()>

Add cookies to this context.

Source

pub async fn cookies(&self) -> Result<Vec<Value>>

Get cookies for this context.

Source

pub async fn clear_cookies(&self) -> Result<()>

Clear cookies for this context.

Source

pub fn on_page<F, Fut>(&self, handler: F)
where F: Fn(Page) -> Fut + Send + Sync + 'static, Fut: Future<Output = ()> + Send + 'static,

Register a handler called for each new page in this context.

Source

pub async fn close(&self) -> Result<()>

Close the context and all its pages. Disposes an isolated context.

Source

pub async fn grant_permissions(&self, permissions: &[&str]) -> Result<()>

Grant browser permissions (e.g. ["geolocation", "clipboard-read"]). Applies browser-wide (CDP Browser.grantPermissions is not context-scoped).

Source

pub async fn clear_permissions(&self) -> Result<()>

Reset all granted permissions.

Source

pub async fn storage_state(&self) -> Result<StorageState>

Snapshot storage state: cookies plus per-origin localStorage.

For each page in the context, localStorage is captured grouped by location.origin. Pages whose origin is "null" (e.g. about:blank, data: URLs) are skipped.

Source

pub async fn set_storage_state(&self, state: &StorageState) -> Result<()>

Restore storage state previously captured by Self::storage_state: cookies are added via the Storage domain, and each origin’s localStorage is set by evaluating localStorage.setItem on a page at that origin (an existing page if one matches, otherwise a freshly created one).

Source

pub async fn route<F, Fut>(&self, pattern: &str, handler: F) -> Result<()>
where F: Fn(Route) -> Fut + Send + Sync + 'static, Fut: Future<Output = ()> + Send + 'static,

Register a route that intercepts matching requests on ALL pages in this context — including pages created after this call. The handler must continue/fulfill/abort the Route.

A context route is applied to each page by registering the equivalent page-level route on it. Context-level and page-level routes are otherwise independent.

Source

pub async fn unroute(&self, pattern: &str) -> Result<()>

Remove the first context-level route registered with exactly pattern and remove the matching page-level route on each page.

Source

pub async fn unroute_all(&self) -> Result<()>

Remove all context-level routes and all page-level routes on each page.

Source

pub fn tracing(&self) -> Tracing

A performance tracing handle bound to the browser-level CDP session.

Mirrors Playwright’s context.tracing(). The Tracing domain is browser-level; tracing() returns clones of a single shared handle so [Tracing::start] and [Tracing::stop] operate on the same state.

Trait Implementations§

Source§

impl Clone for BrowserContext

Source§

fn clone(&self) -> BrowserContext

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> PolicyExt for T
where T: ?Sized,

Source§

fn and<P, B, E>(self, other: P) -> And<T, P>
where T: Sized + Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow only if self and other return Action::Follow. Read more
Source§

fn or<P, B, E>(self, other: P) -> Or<T, P>
where T: Sized + Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow if either self or other returns Action::Follow. Read more
Source§

impl<T> Read<Exclusive, BecauseExclusive> for T
where T: ?Sized,

Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V

Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more