Skip to main content

Page

Struct Page 

Source
pub struct Page {
    pub target_id: String,
    pub session_id: String,
    /* private fields */
}
Expand description

A handle to a single page/tab in the browser.

Page provides methods for interacting with a specific page or tab, including navigation, content retrieval, screenshot capture, and element interaction.

§Example

use ferrous_browser::{Browser, WaitUntil};

let browser = Browser::launch().await?;
let page = browser.new_page().await?;

page.goto("https://example.com", WaitUntil::Load).await?;
let html = page.content().await?;
let screenshot = page.screenshot().await?;

Fields§

§target_id: String

Target/page ID

§session_id: String

Session ID for routing CDP commands

Implementations§

Source§

impl Page

Source

pub fn locator(&self, selector: &str) -> Locator

Create a Locator for the given CSS selector.

§Example
let browser = Browser::launch().await?;
let page = browser.new_page().await?;
page.goto("https://example.com", WaitUntil::Load).await?;

page.locator("button#submit").click().await?;
page.locator("input[name=q]").type_text("rust").await?;
page.locator(".result").wait_for().await?;
Source

pub async fn goto(&self, url: &str, wait_until: WaitUntil) -> Result<()>

Navigate to a URL and wait for the specified condition.

§Arguments
  • url — The URL to navigate to
  • wait_until — When to consider navigation complete
§Example
let browser = Browser::launch().await?;
let page = browser.new_page().await?;
page.goto("https://example.com", WaitUntil::Load).await?;
page.goto("https://example.com", WaitUntil::DomContentLoaded).await?;
page.goto("https://example.com", WaitUntil::NetworkIdle).await?;
Source

pub async fn evaluate_handle(&self, expression: &str) -> Result<String>

Evaluate a JavaScript expression and return a remote object handle.

This is useful when you need a reference to a JavaScript object without serializing it back to Rust. The returned handle is valid only for this session and should be disposed of when no longer needed.

§Example
let browser = Browser::launch_chrome(None).await?;
let page = browser.new_page().await?;
page.goto("https://example.com", WaitUntil::Load).await?;
// Get a remote reference to an object
let handle = page.evaluate_handle("document.body").await?;
println!("Remote object handle: {}", handle);
Source

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

Evaluate a JavaScript expression in the page context and deserialize the result as T.

§Example
let browser = Browser::launch_chrome(None).await?;
let page = browser.new_page().await?;
page.goto("https://example.com", WaitUntil::Load).await?;
let title: String = page.evaluate("document.title").await?;
let count: u64 = page.evaluate("document.querySelectorAll('a').length").await?;
Source

pub async fn wait_for_selector(&self, selector: &str) -> Result<()>

Wait for an element matching selector to appear in the DOM.

Uses a 30-second timeout.

Source

pub async fn wait_for_selector_with_timeout( &self, selector: &str, dur: Duration, ) -> Result<()>

Wait for an element matching selector with a custom timeout.

Implementation note: we push the entire wait into Chrome with a MutationObserver-backed Promise and use Runtime.evaluate’s awaitPromise: true so Chrome holds the response until the element appears (or the timer fires). Net result is one CDP round-trip per call and a reaction latency bounded by the DOM mutation that inserted the element, not by a polling interval.

Source

pub async fn click(&self, selector: &str) -> Result<()>

Click an element matching the CSS selector.

Prefer Page::locator for new code.

Source

pub async fn type_text(&self, selector: &str, text: &str) -> Result<()>

Type text into an input element matching the CSS selector.

Prefer Page::locator for new code.

Source

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

Get the full HTML content of the page.

§Example
let browser = Browser::launch().await?;
let page = browser.new_page().await?;
page.goto("https://example.com", WaitUntil::Load).await?;
let html = page.content().await?;
println!("HTML: {}", html);
Source

pub async fn screenshot(&self) -> Result<Vec<u8>>

Take a screenshot of the page and return PNG bytes.

§Example
let browser = Browser::launch().await?;
let page = browser.new_page().await?;
page.goto("https://example.com", WaitUntil::Load).await?;
let png = page.screenshot().await?;
std::fs::write("screenshot.png", png)?;
Source

pub async fn intercept_requests<F>(&self, callback: F) -> Result<()>
where F: Fn(&str, &str) -> bool + Send + 'static,

Intercept network requests matching a pattern.

Enables request interception and calls the callback for matching requests. The callback receives (url, resource_type) and returns true to abort the request.

Source

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

Get all cookies from the page.

Retrieves all cookies visible to the current page, including expired cookies if they are still in the cookie jar.

§Example
let browser = Browser::launch().await?;
let page = browser.new_page().await?;
page.goto("https://example.com", WaitUntil::Load).await?;
let cookies = page.cookies().await?;
for cookie in cookies {
    println!("{}={}", cookie.name, cookie.value);
}
Source

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

Set cookies for the page (session persistence).

Sets one or more cookies that will be visible to JavaScript and HTTP requests. Typically called before navigation to pre-populate cookies for authentication.

§Example
let browser = Browser::launch().await?;
let page = browser.new_page().await?;
let cookies = vec![Cookie {
    name: "session_id".to_string(),
    value: "abc123xyz".to_string(),
    domain: Some("example.com".to_string()),
    ..Default::default()
}];
page.set_cookies(&cookies).await?;
page.goto("https://example.com", WaitUntil::Load).await?;
Source

pub async fn pdf(&self) -> Result<Vec<u8>>

Export the page as PDF and return the bytes.

Converts the current page to PDF format. By default, includes all pages and uses A4 paper size in portrait mode.

§Example
let browser = Browser::launch().await?;
let page = browser.new_page().await?;
page.goto("https://example.com", WaitUntil::Load).await?;
let pdf = page.pdf().await?;
std::fs::write("page.pdf", pdf)?;
Source

pub async fn pdf_with_options(&self, options: Option<&Value>) -> Result<Vec<u8>>

Export the page as PDF with custom options.

Allows control over paper size, margins, scale, landscape mode, and more.

§Example
let browser = Browser::launch().await?;
let page = browser.new_page().await?;
page.goto("https://example.com", WaitUntil::Load).await?;
let options = json!({
    "landscape": true,
    "scale": 1.5,
    "paperWidth": 11.0,
    "paperHeight": 8.5,
});
let pdf = page.pdf_with_options(Some(&options)).await?;
Source

pub async fn start_har_capture(&self) -> Result<HarCapture>

Start capturing HTTP Archive (HAR) data for this page.

Enables the Network domain and begins collecting request/response entries. Use HarCapture::stop to get the complete HAR archive, or HarCapture::export for a snapshot while continuing to capture.

§Example
let browser = Browser::launch_chrome(None).await?;
let page = browser.new_page().await?;

let mut har = page.start_har_capture().await?;
page.goto("https://example.com", WaitUntil::Load).await?;

let archive = har.stop().await;
let json = serde_json::to_string_pretty(&archive)?;

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 Freeze for Page

§

impl !RefUnwindSafe for Page

§

impl Send for Page

§

impl Sync for Page

§

impl Unpin for Page

§

impl UnsafeUnpin for Page

§

impl !UnwindSafe 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<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> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
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