Tab

Struct Tab 

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

A handle to a browser tab.

Tabs provide methods for navigation, scripting, and element interaction.

Implementations§

Source§

impl Tab

Source

pub fn tab_id(&self) -> TabId

Returns the tab ID.

Source

pub fn frame_id(&self) -> FrameId

Returns the current frame ID.

Source

pub fn session_id(&self) -> SessionId

Returns the session ID.

Source

pub fn is_main_frame(&self) -> bool

Checks if currently in the main frame.

Source§

impl Tab

Source

pub async fn find_element(&self, by: By) -> Result<Element>

Finds a single element using a locator strategy.

§Example
use firefox_webdriver::By;

// CSS selector
let btn = tab.find_element(By::Css("#submit")).await?;

// By ID
let form = tab.find_element(By::Id("login-form")).await?;

// By text content
let link = tab.find_element(By::Text("Click here")).await?;

// By XPath
let btn = tab.find_element(By::XPath("//button[@type='submit']")).await?;
Source

pub async fn find_elements(&self, by: By) -> Result<Vec<Element>>

Finds all elements using a locator strategy.

§Example
use firefox_webdriver::By;

let buttons = tab.find_elements(By::Tag("button")).await?;
let links = tab.find_elements(By::PartialText("Read")).await?;
Source§

impl Tab

Source

pub async fn wait_for_element(&self, by: By) -> Result<Element>

Waits for an element using a locator strategy.

Uses MutationObserver (no polling). Times out after 30 seconds.

§Example
use firefox_webdriver::By;

let btn = tab.wait_for_element(By::Id("submit")).await?;
let link = tab.wait_for_element(By::Css("a.login")).await?;
let el = tab.wait_for_element(By::XPath("//button")).await?;
Source

pub async fn wait_for_element_timeout( &self, by: By, timeout_duration: Duration, ) -> Result<Element>

Waits for an element using a locator strategy with custom timeout.

Source

pub async fn on_element_added<F>( &self, by: By, callback: F, ) -> Result<SubscriptionId>
where F: Fn(Element) + Send + Sync + 'static,

Registers a callback for when elements matching the selector appear.

§Returns

Subscription ID for later unsubscription.

Source

pub async fn on_element_removed<F>( &self, element_id: &ElementId, callback: F, ) -> Result<()>
where F: Fn() + Send + Sync + 'static,

Registers a callback for when a specific element is removed.

Source

pub async fn unsubscribe(&self, subscription_id: &SubscriptionId) -> Result<()>

Unsubscribes from element observation.

Source§

impl Tab

Source

pub async fn switch_to_frame(&self, iframe: &Element) -> Result<Tab>

Switches to a frame by iframe element.

Returns a new Tab handle with the updated frame context.

§Arguments
  • iframe - Element reference to an iframe
§Example
let iframe = tab.find_element("iframe#content").await?;
let frame_tab = tab.switch_to_frame(&iframe).await?;
Source

pub async fn switch_to_frame_by_index(&self, index: usize) -> Result<Tab>

Switches to a frame by index (0-based).

§Arguments
  • index - Zero-based index of the frame
Source

pub async fn switch_to_frame_by_url(&self, url_pattern: &str) -> Result<Tab>

Switches to a frame by URL pattern.

Supports wildcards (* for any characters, ? for single character).

§Arguments
  • url_pattern - URL pattern with optional wildcards
Source

pub async fn switch_to_parent_frame(&self) -> Result<Tab>

Switches to the parent frame.

Source

pub fn switch_to_main_frame(&self) -> Tab

Switches to the main (top-level) frame.

Source

pub async fn get_frame_count(&self) -> Result<usize>

Gets the count of direct child frames.

Source

pub async fn get_all_frames(&self) -> Result<Vec<FrameInfo>>

Gets information about all frames in the tab.

Source§

impl Tab

Source

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

Navigates to a URL.

§Arguments
  • url - The URL to navigate to
§Errors

Returns an error if navigation fails.

Source

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

Loads HTML content directly into the page.

Useful for testing with inline HTML without needing a server.

§Arguments
  • html - HTML content to load
§Example
tab.load_html("<html><body><h1>Test</h1></body></html>").await?;
Source

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

Reloads the current page.

Source

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

Navigates back in history.

Source

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

Navigates forward in history.

Source

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

Gets the current page title.

Source

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

Gets the current URL.

Source

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

Focuses this tab (makes it active).

Source

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

Focuses the window containing this tab.

Source

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

Closes this tab.

Source

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

Gets the page source HTML.

Source§

impl Tab

Source

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

Sets URL patterns to block.

Patterns support wildcards (*).

§Example
tab.set_block_rules(&["*ads*", "*tracking*"]).await?;
Source

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

Clears all URL block rules.

Source

pub async fn intercept_request<F>(&self, callback: F) -> Result<InterceptId>
where F: Fn(InterceptedRequest) -> RequestAction + Send + Sync + 'static,

Intercepts network requests with a callback.

§Returns

An InterceptId that can be used to stop this intercept.

§Example
use firefox_webdriver::RequestAction;

let id = tab.intercept_request(|req| {
    if req.url.contains("ads") {
        RequestAction::block()
    } else {
        RequestAction::allow()
    }
}).await?;
Source

pub async fn intercept_request_headers<F>( &self, callback: F, ) -> Result<InterceptId>
where F: Fn(InterceptedRequestHeaders) -> HeadersAction + Send + Sync + 'static,

Intercepts request headers with a callback.

Source

pub async fn intercept_request_body<F>( &self, callback: F, ) -> Result<InterceptId>
where F: Fn(InterceptedRequestBody) + Send + Sync + 'static,

Intercepts request body for logging (read-only).

Source

pub async fn intercept_response<F>(&self, callback: F) -> Result<InterceptId>
where F: Fn(InterceptedResponse) -> HeadersAction + Send + Sync + 'static,

Intercepts response headers with a callback.

Source

pub async fn intercept_response_body<F>( &self, callback: F, ) -> Result<InterceptId>
where F: Fn(InterceptedResponseBody) -> BodyAction + Send + Sync + 'static,

Intercepts response body with a callback.

Source

pub async fn stop_intercept(&self, intercept_id: &InterceptId) -> Result<()>

Stops network interception.

§Arguments
  • intercept_id - The intercept ID returned from intercept methods
Source§

impl Tab

Source

pub async fn set_proxy(&self, config: ProxyConfig) -> Result<()>

Sets a proxy for this tab.

Tab-level proxy overrides window-level proxy for this tab only.

§Example
use firefox_webdriver::ProxyConfig;

tab.set_proxy(ProxyConfig::http("proxy.example.com", 8080)).await?;
Source

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

Clears the proxy for this tab.

Source§

impl Tab

Source

pub fn screenshot(&self) -> ScreenshotBuilder<'_>

Creates a screenshot builder for capturing page screenshots.

Uses the browser’s native screenshot API for accurate pixel capture.

§Example
// PNG screenshot as base64
let data = tab.screenshot().png().capture().await?;

// JPEG screenshot saved to file
tab.screenshot().jpeg(85).save("page.jpg").await?;
Source

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

Captures a PNG screenshot and returns base64-encoded data.

Shorthand for tab.screenshot().png().capture().await.

Source

pub async fn save_screenshot(&self, path: impl AsRef<Path>) -> Result<()>

Captures a screenshot and saves to a file.

Format is determined by file extension (.png or .jpg/.jpeg).

Source§

impl Tab

Source

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

Executes synchronous JavaScript in the page context.

The script should use return to return a value.

§Example
let title = tab.execute_script("return document.title").await?;
Source

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

Executes asynchronous JavaScript in the page context.

The script should return a Promise or use async/await.

Source§

impl Tab

Source

pub async fn scroll_by(&self, x: i32, y: i32) -> Result<()>

Scrolls the page by the specified amount.

§Arguments
  • x - Horizontal scroll amount in pixels (positive = right)
  • y - Vertical scroll amount in pixels (positive = down)
§Example
// Scroll down 500 pixels
tab.scroll_by(0, 500).await?;

// Scroll right 200 pixels
tab.scroll_by(200, 0).await?;
Source

pub async fn scroll_to(&self, x: i32, y: i32) -> Result<()>

Scrolls the page to the specified position.

§Arguments
  • x - Horizontal position in pixels from left
  • y - Vertical position in pixels from top
§Example
// Scroll to top of page
tab.scroll_to(0, 0).await?;

// Scroll to position (100, 500)
tab.scroll_to(100, 500).await?;
Source

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

Scrolls to the top of the page.

Source

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

Scrolls to the bottom of the page.

Source

pub async fn get_scroll_position(&self) -> Result<(i32, i32)>

Gets the current scroll position.

§Returns

Tuple of (x, y) scroll position in pixels.

Source

pub async fn get_page_size(&self) -> Result<(i32, i32)>

Gets the page dimensions (scrollable area).

§Returns

Tuple of (width, height) in pixels.

Source

pub async fn get_viewport_size(&self) -> Result<(i32, i32)>

Gets the viewport dimensions.

§Returns

Tuple of (width, height) in pixels.

Source§

impl Tab

Gets a cookie by name.

Sets a cookie.

§Example
use firefox_webdriver::Cookie;

tab.set_cookie(Cookie::new("session", "abc123")).await?;

Deletes a cookie by name.

Source

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

Gets all cookies for the current page.

Source§

impl Tab

Source

pub async fn local_storage_get(&self, key: &str) -> Result<Option<String>>

Gets a value from localStorage.

Source

pub async fn local_storage_set(&self, key: &str, value: &str) -> Result<()>

Sets a value in localStorage.

Source

pub async fn local_storage_delete(&self, key: &str) -> Result<()>

Deletes a key from localStorage.

Source

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

Clears all localStorage.

Source§

impl Tab

Source

pub async fn session_storage_get(&self, key: &str) -> Result<Option<String>>

Gets a value from sessionStorage.

Source

pub async fn session_storage_set(&self, key: &str, value: &str) -> Result<()>

Sets a value in sessionStorage.

Source

pub async fn session_storage_delete(&self, key: &str) -> Result<()>

Deletes a key from sessionStorage.

Source

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

Clears all sessionStorage.

Trait Implementations§

Source§

impl Clone for Tab

Source§

fn clone(&self) -> Tab

Returns a duplicate of the value. Read more
1.0.0 · Source§

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

Performs copy-assignment from source. Read more
Source§

impl Debug for Tab

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

§

impl Freeze for Tab

§

impl !RefUnwindSafe for Tab

§

impl Send for Tab

§

impl Sync for Tab

§

impl Unpin for Tab

§

impl !UnwindSafe for Tab

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> 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