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: StringTarget/page ID
session_id: StringSession ID for routing CDP commands
Implementations§
Source§impl Page
impl Page
Sourcepub fn locator(&self, selector: &str) -> Locator
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?;Sourcepub async fn goto(&self, url: &str, wait_until: WaitUntil) -> Result<()>
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 towait_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?;Sourcepub async fn evaluate_handle(&self, expression: &str) -> Result<String>
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);Sourcepub async fn evaluate<T: DeserializeOwned>(&self, expression: &str) -> Result<T>
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?;Sourcepub async fn wait_for_selector(&self, selector: &str) -> Result<()>
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.
Sourcepub async fn wait_for_selector_with_timeout(
&self,
selector: &str,
dur: Duration,
) -> Result<()>
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.
Sourcepub async fn click(&self, selector: &str) -> Result<()>
pub async fn click(&self, selector: &str) -> Result<()>
Click an element matching the CSS selector.
Prefer Page::locator for new code.
Sourcepub async fn type_text(&self, selector: &str, text: &str) -> Result<()>
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.
Sourcepub async fn content(&self) -> Result<String>
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);Sourcepub async fn screenshot(&self) -> Result<Vec<u8>>
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)?;Sourcepub async fn intercept_requests<F>(&self, callback: F) -> Result<()>
pub async fn intercept_requests<F>(&self, callback: F) -> Result<()>
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.
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);
}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?;Sourcepub async fn pdf(&self) -> Result<Vec<u8>>
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)?;Sourcepub async fn pdf_with_options(&self, options: Option<&Value>) -> Result<Vec<u8>>
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?;Sourcepub async fn start_har_capture(&self) -> Result<HarCapture>
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§
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> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
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 moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
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