pub struct ElementHandle { /* private fields */ }Implementations§
Source§impl ElementHandle
impl ElementHandle
Sourcepub async fn left_click(&self) -> Result<()>
pub async fn left_click(&self) -> Result<()>
Left-clicks the element.
Sourcepub async fn right_click(&self) -> Result<()>
pub async fn right_click(&self) -> Result<()>
Right-clicks the element.
Sourcepub async fn hover(&self) -> Result<MousePosition>
pub async fn hover(&self) -> Result<MousePosition>
Moves the mouse to the element center and returns the resulting pointer position.
Sourcepub async fn move_mouse_to(&self) -> Result<MousePosition>
pub async fn move_mouse_to(&self) -> Result<MousePosition>
Semantic alias for Self::hover.
Sourcepub async fn double_click(&self) -> Result<()>
pub async fn double_click(&self) -> Result<()>
Double-clicks the element.
Sourcepub async fn press_and_hold(&self, duration: Duration) -> Result<()>
pub async fn press_and_hold(&self, duration: Duration) -> Result<()>
Presses and holds the element for the provided duration using the left mouse button.
Sourcepub async fn press_and_hold_until<F, Fut>(
&self,
options: PressHoldOptions,
condition: F,
) -> Result<bool>
pub async fn press_and_hold_until<F, Fut>( &self, options: PressHoldOptions, condition: F, ) -> Result<bool>
Presses and holds the element until the provided condition returns
true or the configured timeout elapses.
Sourcepub async fn text_content(&self) -> Result<String>
pub async fn text_content(&self) -> Result<String>
Gets the text content of the element.
Sourcepub async fn type_text(&self, text: &str) -> Result<()>
pub async fn type_text(&self, text: &str) -> Result<()>
Inserts the entire text payload into the element in a single CDP call.
The element is focused before invoking Input.insertText, which means
the input behaves like a fast paste action instead of character-by-character typing.
§Parameters
text- The text to insert.
§Examples
if let Some(input) = page.query_selector("input[type='text']").await? {
input.type_text("Hello, World!").await?;
}Sourcepub async fn upload_files<P>(
&self,
files: impl IntoIterator<Item = P>,
) -> Result<()>
pub async fn upload_files<P>( &self, files: impl IntoIterator<Item = P>, ) -> Result<()>
Opens the file chooser dialog and populates it with the provided paths.
Sourcepub async fn clear(&self) -> Result<()>
pub async fn clear(&self) -> Result<()>
Clears the element’s value or selection state (input, textarea, select, form, and contentEditable are supported).
This implementation mimics real user interactions where possible:
- Text inputs and textareas reset
value = ""and emitinput/change - Checkboxes and radio buttons are unchecked
- Select elements drop the active selection
- ContentEditable nodes erase their HTML contents
- Form elements recursively clear all editable descendants
Returns an error when an <input type="file"> is encountered to match
browser-level restrictions.
Sourcepub async fn type_text_with_delay(
&self,
text: &str,
min_delay_ms: u64,
max_delay_ms: u64,
) -> Result<()>
pub async fn type_text_with_delay( &self, text: &str, min_delay_ms: u64, max_delay_ms: u64, ) -> Result<()>
Types the provided text character by character while applying a random delay between each keystroke.
The element is first focused and then receives keyDown/keyUp events for
every character. The delay for each character is randomly chosen within
[min_delay_ms, max_delay_ms].
§Parameters
text- Text to type into the element.min_delay_ms- Minimum delay (milliseconds) between characters.max_delay_ms- Maximum delay (milliseconds) between characters.
§Examples
if let Some(input) = page.query_selector("input[type='text']").await? {
input.type_text_with_delay("Hello, World!", 50, 150).await?;
}Sourcepub async fn get_attribute(
&self,
attribute_name: &str,
) -> Result<Option<String>>
pub async fn get_attribute( &self, attribute_name: &str, ) -> Result<Option<String>>
Gets the value of an attribute on the element, if present.
§Parameters
attribute_name- The attribute to read (for exampleid,class,data-value).
§Returns
Some(String)when the attribute is defined.Nonewhen the attribute is missing or resolves tonull.
§Examples
if let Some(element) = page.query_selector("div.example").await? {
if let Some(id) = element.get_attribute("id").await? {
println!("Element ID: {}", id);
}
if let Some(data_value) = element.get_attribute("data-value").await? {
println!("Data value: {}", data_value);
}
}Sourcepub async fn screenshot(&self, save_path: Option<PathBuf>) -> Result<String>
pub async fn screenshot(&self, save_path: Option<PathBuf>) -> Result<String>
Takes a screenshot of the element.
§Parameters
save_path- Optional file path (including file name). WhenNone, a timestamped file such aselement_screenshot_<ts>.pngis created in the current working directory.
§Returns
The path where the screenshot was saved.
§Notes
- Uses the default border box, which generally works well for rounded elements.
- Automatically adapts to the device pixel ratio (DPR) for crisp images.
- Use
screenshot_with_optionsfor additional control.
§Examples
if let Some(element) = page.query_selector("div.example").await? {
// Save to the current directory (with DPR auto-adjust)
let path = element.screenshot(None).await?;
println!("Element screenshot saved to: {}", path);
// Save to a custom location
let path = element.screenshot(Some("screenshots/element.png".into())).await?;
println!("Element screenshot saved to: {}", path);
}Sourcepub async fn screenshot_with_options(
&self,
save_path: Option<PathBuf>,
box_type: ScreenshotBoxType,
auto_resolve_dpr: bool,
) -> Result<String>
pub async fn screenshot_with_options( &self, save_path: Option<PathBuf>, box_type: ScreenshotBoxType, auto_resolve_dpr: bool, ) -> Result<String>
Takes a screenshot of the element with a custom box type.
§Parameters
save_path- Optional file path (including file name).box_type- Bounding box strategy that determines the capture region.auto_resolve_dpr- Whether to adapt the screenshot to the device pixel ratio (trueis recommended for high-DPI displays).
§Bounding Box Types
-
Content: Captures only the content area, excluding padding.- Best for: screenshots that should omit interior padding.
- Less ideal when padding needs to remain visible.
-
Padding: Captures content and padding, excluding the border.- Best for: including interior spacing while omitting borders.
- Less ideal when borders or rounded corners must be preserved.
-
Border(default): Captures content, padding, and border.- Ideal for rounded corners (
border-radius). - Suitable for elements where the border defines the visual shape.
- Works well for most everyday use cases.
- Ideal for rounded corners (
-
Margin: Captures content, padding, border, and margin.- Best for: including surrounding whitespace in the capture.
- Less ideal when margins introduce too much empty space.
§Returns
The path where the screenshot was saved.
§Examples
if let Some(element) = page.query_selector("button.rounded").await? {
// Rounded button with border box + DPR auto adjustment (recommended)
let path = element.screenshot_with_options(
Some("button.png".into()),
ScreenshotBoxType::Border,
true // enable DPR auto adjustment
).await?;
// Content-only capture with DPR adaptation disabled
let path = element.screenshot_with_options(
Some("content.png".into()),
ScreenshotBoxType::Content,
false // fixed scale = 1.0
).await?;
// Include the margin while keeping DPR auto adjustment
let path = element.screenshot_with_options(
Some("with-margin.png".into()),
ScreenshotBoxType::Margin,
true
).await?;
}Sourcepub async fn is_visible(&self) -> Result<bool>
pub async fn is_visible(&self) -> Result<bool>
Determines whether the element is visible.
The element is considered visible when:
- It is present in the DOM tree.
- It does not use
display: noneorvisibility: hidden(andopacityis not0). - Its rendered bounding box is non-zero.
§Returns
true if the element is visible, otherwise false.
§Examples
let element = page.query_selector("#my-element").await?.unwrap();
if element.is_visible().await? {
println!("Element is visible");
}Sourcepub async fn is_enabled(&self) -> Result<bool>
pub async fn is_enabled(&self) -> Result<bool>
Checks whether the element is enabled (not disabled).
§Returns
true when the element is enabled, otherwise false.
Sourcepub async fn is_clickable(&self) -> Result<bool>
pub async fn is_clickable(&self) -> Result<bool>
Determines whether the element can be clicked.
The element is considered clickable when it is visible, enabled, and not occluded by another element at its center point.
§Returns
true if the element is clickable, otherwise false.
Waits for the element to become hidden.
§Parameters
timeout_ms- Timeout in milliseconds (defaults to30000).
Sourcepub async fn wait_for_enabled(&self, timeout_ms: Option<u64>) -> Result<()>
pub async fn wait_for_enabled(&self, timeout_ms: Option<u64>) -> Result<()>
Waits for the element to become enabled (not disabled).
§Parameters
timeout_ms- Timeout in milliseconds (defaults to30000).
Sourcepub async fn shadow_root(&self) -> Result<Option<ShadowRoot>>
pub async fn shadow_root(&self) -> Result<Option<ShadowRoot>>
Retrieves the element’s shadow root (supports both open and closed modes).
§Returns
Ok(Some(ShadowRoot))when the element exposes a shadow root.Ok(None)when the element has no shadow root.Err(_)if the operation failed.
§Examples
let host = page.query_selector("#shadow-host").await?.unwrap();
if let Some(shadow_root) = host.shadow_root().await? {
// Query inside the shadow DOM
let inner = shadow_root.query_selector(".inner-element").await?;
}Sourcepub async fn query_selector_shadow(
&self,
selector: &str,
) -> Result<Option<ElementHandle>>
pub async fn query_selector_shadow( &self, selector: &str, ) -> Result<Option<ElementHandle>>
Queries for a single element inside the shadow DOM (supports open and closed modes).
This is a convenience wrapper around element.shadow_root().await?.query_selector(selector).
§Parameters
selector- CSS selector.
§Returns
Ok(Some(ElementHandle))when a matching element is found.Ok(None)when the element has no shadow root or no match is located.Err(_)if querying fails.
§Examples
let host = page.query_selector("#shadow-host").await?.unwrap();
if let Some(inner) = host.query_selector_shadow(".inner-element").await? {
let text = inner.text_content().await?;
println!("Shadow DOM content: {}", text);
}Sourcepub async fn query_selector_all_shadow(
&self,
selector: &str,
) -> Result<Vec<ElementHandle>>
pub async fn query_selector_all_shadow( &self, selector: &str, ) -> Result<Vec<ElementHandle>>
Queries for all matching elements within the shadow DOM (supports open and closed modes).
§Parameters
selector- CSS selector used for the lookup.
§Returns
A vector of matching elements, or an empty vector when no shadow root is present.
§Examples
let host = page.query_selector("#shadow-host").await?.unwrap();
let items = host.query_selector_all_shadow(".list-item").await?;
println!("Found {} items in Shadow DOM", items.len());Trait Implementations§
Source§impl Clone for ElementHandle
impl Clone for ElementHandle
Source§fn clone(&self) -> ElementHandle
fn clone(&self) -> ElementHandle
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read more