Skip to main content

Page

Struct Page 

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

A page (tab) in a browser.

Implementations§

Source§

impl Page

Source

pub fn browser(&self) -> Browser

Source

pub fn target_id(&self) -> &str

Source

pub fn is_closed(&self) -> bool

Source

pub async fn goto( &self, url: &str, opts: Option<GotoOptions>, ) -> Result<Option<Response>>

Navigate to url.

Source

pub async fn reload( &self, opts: Option<GotoOptions>, ) -> Result<Option<Response>>

Reload the page.

Source

pub async fn wait_for_load_state(&self, state: Option<WaitUntil>) -> Result<()>

Wait for a given document lifecycle state.

Source

pub async fn go_back( &self, opts: Option<GotoOptions>, ) -> Result<Option<Response>>

Navigate one entry back in history (best-effort; returns no response).

Source

pub async fn go_forward( &self, opts: Option<GotoOptions>, ) -> Result<Option<Response>>

Navigate one entry forward in history (best-effort; returns no response).

Source

pub async fn wait_for_url( &self, url: &str, opts: Option<GotoOptions>, ) -> Result<()>

Wait until the page URL matches url (exact, or * glob), then optionally wait for a lifecycle state.

Source

pub fn set_default_navigation_timeout(&self, ms: u64)

Set the default navigation timeout (ms), separate from action timeout.

Source

pub async fn emulate_media( &self, opts: Option<EmulateMediaOptions>, ) -> Result<()>

Emulate CSS media (media / color-scheme / reduced-motion).

Source

pub async fn set_offline(&self, offline: bool) -> Result<()>

Toggle network offline emulation.

Source

pub async fn drag_and_drop( &self, source: &str, target: &str, options: Option<DragToOptions>, ) -> Result<()>

Drag the element matched by source onto the element matched by target.

Source

pub async fn url(&self) -> Result<String>

Source

pub async fn title(&self) -> Result<String>

Source

pub async fn content(&self) -> Result<String>

Source

pub async fn set_content(&self, html: &str) -> Result<()>

Source

pub async fn evaluate<R: DeserializeOwned>(&self, expression: &str) -> Result<R>

Evaluate a JS expression in the main world, returning a typed result.

expression is wrapped as (arg) => { return (<expression>); }.

Source

pub async fn evaluate_with_arg<R: DeserializeOwned, T: Serialize>( &self, expression: &str, arg: &T, ) -> Result<R>

Evaluate a JS expression with a JSON-serializable argument.

Source

pub async fn wait_for_function<R: DeserializeOwned>( &self, expression: &str, arg: Option<Value>, options: Option<WaitForFunctionOptions>, ) -> Result<R>

Poll expression until it evaluates to a truthy value, then return the deserialized result. Mirrors Playwright’s page.waitForFunction.

expression is wrapped as (arg) => { return (<expression>); } (the same form as Page::evaluate). A value is considered truthy unless it is null, false, 0, or "". On timeout (default 30s) this returns Error::Timeout. Polls every polling_interval ms (default 100).

Source

pub async fn eval_on_selector<R: DeserializeOwned>( &self, selector: &str, expression: &str, ) -> Result<R>

Evaluate expression against the first element matching selector.

Source

pub async fn eval_on_selector_all<R: DeserializeOwned>( &self, selector: &str, expression: &str, ) -> Result<Vec<R>>

Evaluate expression against all elements matching selector.

Source

pub async fn set_viewport_size(&self, viewport: Viewport) -> Result<()>

Source

pub async fn screenshot( &self, opts: Option<ScreenshotOptions>, ) -> Result<Vec<u8>>

Source

pub async fn add_script_tag( &self, content: Option<&str>, url: Option<&str>, ) -> Result<()>

Inject a <script> tag into the page (inline content and/or url).

Source

pub async fn add_style_tag(&self, content: &str) -> Result<()>

Inject a <style> tag into the page.

Source

pub async fn set_cache_disabled(&self, disabled: bool) -> Result<()>

Enable/disable the HTTP cache for this page.

Source

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

Bring the page to the front (focus).

Source

pub async fn set_geolocation( &self, geolocation: Option<(f64, f64)>, ) -> Result<()>

Override geolocation (latitude, longitude) (or clear with None). Requires a granted geolocation permission.

Source

pub fn request(&self) -> APIRequestContext

Return a standalone HTTP client (APIRequestContext) tied to this page. The client shares no state with the browser tab — it is a direct HTTP client useful for API testing.

Note: the page does not retain its extra_http_headers in Rust (they are applied directly to CDP), so the returned context starts with an empty default header set. Use [BrowserContext::request] to seed defaults from the context.

Source

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

Source

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

Source

pub async fn expose_function<F, Fut>( &self, name: &str, callback: F, ) -> Result<()>
where F: Fn(Vec<Value>) -> Fut + Send + Sync + 'static, Fut: Future<Output = Value> + Send + 'static,

Expose a Rust function to the page as window[name].

After this returns, page JS can call await window.<name>(...args) and receive the Rust callback’s return value (serialized as JSON). Mirrors Playwright’s page.exposeFunction.

The callback receives the JS arguments as a Vec<serde_json::Value> and returns a serde_json::Value. If the callback panics or its result fails to serialize, the JS promise resolves with { "__error": "<message>" } rather than rejecting.

§Mechanism

A CDP binding is registered under the internal name __pwcdpInvoke_<name> via Runtime.addBinding. A JS wrapper turns window[name] into a Promise-returning function that forwards { id, args } to that binding (as a JSON string). A per-registration listener handles Runtime.bindingCalled, invokes the callback on its own task, and resolves the Promise by evaluating against the pending-callback map.

The wrapper is installed for future documents via Page.addScriptToEvaluateOnNewDocument and once in the current context. Bindings are per-execution-context and reset on navigation: the wrapper is re-installed on every new document, but Runtime.addBinding is only re-asserted lazily if needed (it persists across same-origin navigations on a live page session; call expose_function again after a cross-origin navigation if the binding goes missing).

Source

pub fn locator(&self, selector: impl Into<String>) -> Locator

Source

pub fn frame_locator(&self, selector: impl Into<String>) -> FrameLocator

Return a FrameLocator scoped to the same-origin <iframe> matched by selector. Element queries on the returned locator resolve inside the iframe’s contentDocument.

Same-origin only. Cross-origin iframes cannot be reached from the page’s main world; their contentDocument reads as null, and queries against them will report zero matches. srcdoc iframes and same-origin src iframes are supported.

Source

pub fn get_by_text(&self, text: &str, _exact: bool) -> Locator

Source

pub fn get_by_label(&self, text: &str) -> Locator

Source

pub fn get_by_placeholder(&self, text: &str) -> Locator

Source

pub fn get_by_alt_text(&self, text: &str) -> Locator

Source

pub fn get_by_title(&self, text: &str) -> Locator

Source

pub fn get_by_test_id(&self, text: &str) -> Locator

Source

pub fn get_by_role( &self, role: AriaRole, opts: Option<GetByRoleOptions>, ) -> Locator

Source

pub async fn query_selector( &self, selector: &str, ) -> Result<Option<ElementHandle>>

The first element matching selector, if any.

Source

pub async fn query_selector_all( &self, selector: &str, ) -> Result<Vec<ElementHandle>>

All elements matching selector.

Source

pub fn keyboard(&self) -> Keyboard

Source

pub fn mouse(&self) -> Mouse

Source

pub fn touchscreen(&self) -> Touchscreen

Source

pub fn main_frame(&self) -> Frame

Source

pub async fn frames(&self) -> Result<Vec<Frame>>

All frames in the page (refreshed from the browser).

Source

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

Source

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

Source

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

Register a handler invoked for each network request sent.

Source

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

Register a handler invoked for each network response received.

Source

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

Register a handler invoked when a network request fails.

Source

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

Register a handler invoked when an uncaught page error occurs.

Source

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

Register a handler invoked when a network request finishes loading.

Source

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

Register a handler invoked when the page closes.

Source

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

Register a handler invoked for each file download (Page.downloadWillBegin).

On first registration, downloads are routed to a per-page temp directory via Page.setDownloadBehavior allow. Progress events update the download’s shared state so Download::path / Download::save_as can await completion.

Source

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

Register a handler invoked when a file-chooser dialog opens (Page.fileChooserOpened), mirroring Playwright’s page.on('filechooser').

On first registration, chooser interception is enabled once via Page.setInterceptFileChooserDialog { enabled: true } (must be in place before the action that opens the chooser). The handler may inspect the FileChooser and call FileChooser::set_files to accept it.

Source

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

Register a handler invoked when a web/service/shared worker is created, mirroring Playwright’s page.on('worker').

On first registration, flattened auto-attach is enabled on the page session (Target.setAutoAttach { flatten: true }) so workers show up as child sessions on the same connection. Each child target of type worker/service_worker/shared_worker surfaces via Target.attachedToTarget and is wrapped in a Worker.

Subscribe before enabling so the first attachedToTarget is never missed.

Source

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

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,

Intercept requests matching pattern (a URL glob, *-wildcarded) and route them to handler. The handler must continue/fulfill/abort the Route (an unhandled route stalls the request).

Source

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

Remove the first route registered with exactly pattern.

Source

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

Remove all routes.

Source

pub async fn pdf(&self, opts: Option<PdfOptions>) -> Result<Vec<u8>>

Source

pub async fn aria_snapshot(&self) -> Result<String>

Capture an aria-snapshot (Playwright’s YAML-ish accessibility-tree format) of the whole page.

Fetches the full accessibility tree via Accessibility.getFullAXTree and serializes it with [crate::aria_snapshot]. The Accessibility domain is enabled best-effort first (some Chrome builds require it).

Trait Implementations§

Source§

impl Clone for Page

Source§

fn clone(&self) -> Page

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§

§

impl !RefUnwindSafe for Page

§

impl !UnwindSafe for Page

§

impl Freeze for Page

§

impl Send for Page

§

impl Sync for Page

§

impl Unpin for Page

§

impl UnsafeUnpin for Page

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