pub struct Page { /* private fields */ }Expand description
A page (tab) in a browser.
Implementations§
Source§impl Page
impl Page
pub fn browser(&self) -> Browser
pub fn target_id(&self) -> &str
pub fn is_closed(&self) -> bool
Sourcepub async fn goto(
&self,
url: &str,
opts: Option<GotoOptions>,
) -> Result<Option<Response>>
pub async fn goto( &self, url: &str, opts: Option<GotoOptions>, ) -> Result<Option<Response>>
Navigate to url.
Sourcepub async fn reload(
&self,
opts: Option<GotoOptions>,
) -> Result<Option<Response>>
pub async fn reload( &self, opts: Option<GotoOptions>, ) -> Result<Option<Response>>
Reload the page.
Sourcepub async fn wait_for_load_state(&self, state: Option<WaitUntil>) -> Result<()>
pub async fn wait_for_load_state(&self, state: Option<WaitUntil>) -> Result<()>
Wait for a given document lifecycle state.
Sourcepub async fn go_back(
&self,
opts: Option<GotoOptions>,
) -> Result<Option<Response>>
pub async fn go_back( &self, opts: Option<GotoOptions>, ) -> Result<Option<Response>>
Navigate one entry back in history (best-effort; returns no response).
Sourcepub async fn go_forward(
&self,
opts: Option<GotoOptions>,
) -> Result<Option<Response>>
pub async fn go_forward( &self, opts: Option<GotoOptions>, ) -> Result<Option<Response>>
Navigate one entry forward in history (best-effort; returns no response).
Sourcepub async fn wait_for_url(
&self,
url: &str,
opts: Option<GotoOptions>,
) -> Result<()>
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.
Set the default navigation timeout (ms), separate from action timeout.
Sourcepub async fn emulate_media(
&self,
opts: Option<EmulateMediaOptions>,
) -> Result<()>
pub async fn emulate_media( &self, opts: Option<EmulateMediaOptions>, ) -> Result<()>
Emulate CSS media (media / color-scheme / reduced-motion).
Sourcepub async fn set_offline(&self, offline: bool) -> Result<()>
pub async fn set_offline(&self, offline: bool) -> Result<()>
Toggle network offline emulation.
Sourcepub async fn drag_and_drop(
&self,
source: &str,
target: &str,
options: Option<DragToOptions>,
) -> Result<()>
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.
pub async fn url(&self) -> Result<String>
pub async fn title(&self) -> Result<String>
pub async fn content(&self) -> Result<String>
pub async fn set_content(&self, html: &str) -> Result<()>
Sourcepub async fn evaluate<R: DeserializeOwned>(&self, expression: &str) -> Result<R>
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>); }.
Sourcepub async fn evaluate_with_arg<R: DeserializeOwned, T: Serialize>(
&self,
expression: &str,
arg: &T,
) -> Result<R>
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.
Sourcepub async fn wait_for_function<R: DeserializeOwned>(
&self,
expression: &str,
arg: Option<Value>,
options: Option<WaitForFunctionOptions>,
) -> Result<R>
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).
Sourcepub async fn eval_on_selector<R: DeserializeOwned>(
&self,
selector: &str,
expression: &str,
) -> Result<R>
pub async fn eval_on_selector<R: DeserializeOwned>( &self, selector: &str, expression: &str, ) -> Result<R>
Evaluate expression against the first element matching selector.
Sourcepub async fn eval_on_selector_all<R: DeserializeOwned>(
&self,
selector: &str,
expression: &str,
) -> Result<Vec<R>>
pub async fn eval_on_selector_all<R: DeserializeOwned>( &self, selector: &str, expression: &str, ) -> Result<Vec<R>>
Evaluate expression against all elements matching selector.
pub async fn set_viewport_size(&self, viewport: Viewport) -> Result<()>
pub async fn screenshot( &self, opts: Option<ScreenshotOptions>, ) -> Result<Vec<u8>>
Sourcepub async fn add_script_tag(
&self,
content: Option<&str>,
url: Option<&str>,
) -> Result<()>
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).
Sourcepub async fn add_style_tag(&self, content: &str) -> Result<()>
pub async fn add_style_tag(&self, content: &str) -> Result<()>
Inject a <style> tag into the page.
Sourcepub async fn set_cache_disabled(&self, disabled: bool) -> Result<()>
pub async fn set_cache_disabled(&self, disabled: bool) -> Result<()>
Enable/disable the HTTP cache for this page.
Sourcepub async fn bring_to_front(&self) -> Result<()>
pub async fn bring_to_front(&self) -> Result<()>
Bring the page to the front (focus).
Sourcepub async fn set_geolocation(
&self,
geolocation: Option<(f64, f64)>,
) -> Result<()>
pub async fn set_geolocation( &self, geolocation: Option<(f64, f64)>, ) -> Result<()>
Override geolocation (latitude, longitude) (or clear with None).
Requires a granted geolocation permission.
Sourcepub fn request(&self) -> APIRequestContext
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.
pub async fn set_extra_http_headers(&self, headers: Headers) -> Result<()>
pub async fn add_init_script(&self, script: &str) -> Result<()>
Sourcepub async fn expose_function<F, Fut>(
&self,
name: &str,
callback: F,
) -> Result<()>
pub async fn expose_function<F, Fut>( &self, name: &str, callback: F, ) -> Result<()>
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).
pub fn locator(&self, selector: impl Into<String>) -> Locator
Sourcepub fn frame_locator(&self, selector: impl Into<String>) -> FrameLocator
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.
pub fn get_by_text(&self, text: &str, _exact: bool) -> Locator
pub fn get_by_label(&self, text: &str) -> Locator
pub fn get_by_placeholder(&self, text: &str) -> Locator
pub fn get_by_alt_text(&self, text: &str) -> Locator
pub fn get_by_title(&self, text: &str) -> Locator
pub fn get_by_test_id(&self, text: &str) -> Locator
pub fn get_by_role( &self, role: AriaRole, opts: Option<GetByRoleOptions>, ) -> Locator
Sourcepub async fn query_selector(
&self,
selector: &str,
) -> Result<Option<ElementHandle>>
pub async fn query_selector( &self, selector: &str, ) -> Result<Option<ElementHandle>>
The first element matching selector, if any.
Sourcepub async fn query_selector_all(
&self,
selector: &str,
) -> Result<Vec<ElementHandle>>
pub async fn query_selector_all( &self, selector: &str, ) -> Result<Vec<ElementHandle>>
All elements matching selector.
pub fn keyboard(&self) -> Keyboard
pub fn mouse(&self) -> Mouse
pub fn touchscreen(&self) -> Touchscreen
pub fn main_frame(&self) -> Frame
Sourcepub async fn frames(&self) -> Result<Vec<Frame>>
pub async fn frames(&self) -> Result<Vec<Frame>>
All frames in the page (refreshed from the browser).
pub fn on_console<F, Fut>(&self, handler: F)
pub fn on_dialog<F, Fut>(&self, handler: F)
Sourcepub fn on_request<F, Fut>(&self, handler: F)
pub fn on_request<F, Fut>(&self, handler: F)
Register a handler invoked for each network request sent.
Sourcepub fn on_response<F, Fut>(&self, handler: F)
pub fn on_response<F, Fut>(&self, handler: F)
Register a handler invoked for each network response received.
Sourcepub fn on_requestfailed<F, Fut>(&self, handler: F)
pub fn on_requestfailed<F, Fut>(&self, handler: F)
Register a handler invoked when a network request fails.
Sourcepub fn on_pageerror<F, Fut>(&self, handler: F)
pub fn on_pageerror<F, Fut>(&self, handler: F)
Register a handler invoked when an uncaught page error occurs.
Sourcepub fn on_requestfinished<F, Fut>(&self, handler: F)
pub fn on_requestfinished<F, Fut>(&self, handler: F)
Register a handler invoked when a network request finishes loading.
Sourcepub fn on_download<F, Fut>(&self, handler: F)
pub fn on_download<F, Fut>(&self, handler: F)
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.
Sourcepub async fn on_filechooser<F, Fut>(&self, handler: F)
pub async fn on_filechooser<F, Fut>(&self, handler: F)
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.
Sourcepub async fn on_worker<F, Fut>(&self, handler: F)
pub async fn on_worker<F, Fut>(&self, handler: F)
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.
pub async fn close(&self) -> Result<()>
Sourcepub async fn route<F, Fut>(&self, pattern: &str, handler: F) -> Result<()>
pub async fn route<F, Fut>(&self, pattern: &str, handler: F) -> Result<()>
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).
Sourcepub async fn unroute(&self, pattern: &str) -> Result<()>
pub async fn unroute(&self, pattern: &str) -> Result<()>
Remove the first route registered with exactly pattern.
Sourcepub async fn unroute_all(&self) -> Result<()>
pub async fn unroute_all(&self) -> Result<()>
Remove all routes.
pub async fn pdf(&self, opts: Option<PdfOptions>) -> Result<Vec<u8>>
Sourcepub async fn aria_snapshot(&self) -> Result<String>
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).