pub struct WebElement {
pub element_id: ElementId,
pub handle: Arc<SessionHandle>,
}Expand description
The WebElement struct encapsulates a single element on a page.
WebElement structs are generally not constructed manually, but rather they are returned from a ‘find_element()’ operation using a WebDriver.
§Example:
let elem = driver.find(By::Id("my-element-id")).await?;You can also search for a child element of another element as follows:
let elem = driver.find(By::Id("my-element-id")).await?;
let child_elem = elem.find(By::Tag("button")).await?;Elements can be clicked using the click() method, and you can send
input to an element using the send_keys() method.
Fields§
§element_id: ElementIdThe element id.
handle: Arc<SessionHandle>The underlying session handle.
Implementations§
Source§impl WebElement
impl WebElement
Sourcepub fn from_json(
value: Value,
handle: Arc<SessionHandle>,
) -> WebDriverResult<Self>
pub fn from_json( value: Value, handle: Arc<SessionHandle>, ) -> WebDriverResult<Self>
Construct a WebElement from a JSON response and a session handle.
The value argument should be a JSON object containing the property
element-6066-11e4-a52e-4f735466cecf whose value is the element id
assigned by the WebDriver.
You can get the session handle from any existing WebDriver or
WebElement that is using this session, e.g. driver.handle.
NOTE: if you simply want to convert a script’s return value to a
WebElement, use ScriptRet::element instead.
Sourcepub fn to_json(&self) -> WebDriverResult<Value>
pub fn to_json(&self) -> WebDriverResult<Value>
Serialize this WebElement to JSON.
This is useful for supplying an element as an argument to a script.
See the documentation for SessionHandle::execute for more details.
Sourcepub fn element_id(&self) -> ElementId
pub fn element_id(&self) -> ElementId
Get the internal element id for this element.
NOTE: If you want the id property of an element,
use WebElement::id instead.
Sourcepub async fn rect(&self) -> WebDriverResult<ElementRect>
pub async fn rect(&self) -> WebDriverResult<ElementRect>
Get the bounding rectangle for this WebElement.
§Example:
let elem = driver.find(By::Id("button1")).await?;
let r = elem.rect().await?;Sourcepub async fn rectangle(&self) -> WebDriverResult<ElementRect>
👎Deprecated since 0.32.0: Use rect() instead
pub async fn rectangle(&self) -> WebDriverResult<ElementRect>
Alias for WebElement::rect().
Sourcepub async fn tag_name(&self) -> WebDriverResult<String>
pub async fn tag_name(&self) -> WebDriverResult<String>
Get the tag name for this WebElement.
§Example:
let elem = driver.find(By::Id("button1")).await?;
assert_eq!(elem.tag_name().await?, "button");Sourcepub async fn class_name(&self) -> WebDriverResult<Option<String>>
pub async fn class_name(&self) -> WebDriverResult<Option<String>>
Get the class name for this WebElement.
§Example:
let elem = driver.find(By::Id("button1")).await?;
let class_name: Option<String> = elem.class_name().await?;Sourcepub async fn id(&self) -> WebDriverResult<Option<String>>
pub async fn id(&self) -> WebDriverResult<Option<String>>
Get the id for this WebElement.
§Example:
let elem = driver.find(By::Id("button1")).await?;
let id: Option<String> = elem.id().await?;Sourcepub async fn text(&self) -> WebDriverResult<String>
pub async fn text(&self) -> WebDriverResult<String>
Get the text contents for this WebElement.
§Example:
let elem = driver.find(By::Id("button1")).await?;
let text = elem.text().await?;
assert_eq!(text, "Click Me");Sourcepub async fn value(&self) -> WebDriverResult<Option<String>>
pub async fn value(&self) -> WebDriverResult<Option<String>>
Convenience method for getting the (optional) value property of this element.
Sourcepub async fn click(&self) -> WebDriverResult<()>
pub async fn click(&self) -> WebDriverResult<()>
Click the WebElement.
§Example:
let elem = driver.find(By::Id("button1")).await?;
elem.click().await?;Sourcepub async fn clear(&self) -> WebDriverResult<()>
pub async fn clear(&self) -> WebDriverResult<()>
Clear the WebElement contents.
§Example:
let elem = driver.find(By::Css("input[type='text']")).await?;
elem.clear().await?;Sourcepub async fn prop(
&self,
name: impl IntoArcStr,
) -> WebDriverResult<Option<String>>
pub async fn prop( &self, name: impl IntoArcStr, ) -> WebDriverResult<Option<String>>
Get the specified property.
§Example:
let elem = driver.find(By::Css("input[type='checkbox']")).await?;
let property_value: Option<String> = elem.prop("checked").await?;
assert_eq!(property_value.unwrap(), "true");
// If a property is not found, None is returned.
assert_eq!(elem.prop("invalid-property").await?, None);Sourcepub async fn get_property(
&self,
name: impl IntoArcStr,
) -> WebDriverResult<Option<String>>
👎Deprecated since 0.30.0: This method has been renamed to prop()
pub async fn get_property( &self, name: impl IntoArcStr, ) -> WebDriverResult<Option<String>>
Get the specified property.
Sourcepub async fn attr(
&self,
name: impl IntoArcStr,
) -> WebDriverResult<Option<String>>
pub async fn attr( &self, name: impl IntoArcStr, ) -> WebDriverResult<Option<String>>
Get the specified attribute.
§Example:
let elem = driver.find(By::Name("input2")).await?;
let attribute: Option<String> = elem.attr("name").await?;
assert_eq!(attribute.unwrap(), "input2");
// If the attribute does not exist, None is returned.
assert_eq!(elem.attr("invalid-attribute").await?, None);Sourcepub async fn get_attribute(
&self,
name: impl IntoArcStr,
) -> WebDriverResult<Option<String>>
👎Deprecated since 0.30.0: This method has been renamed to attr()
pub async fn get_attribute( &self, name: impl IntoArcStr, ) -> WebDriverResult<Option<String>>
Get the specified attribute.
Sourcepub async fn css_value(&self, name: impl IntoArcStr) -> WebDriverResult<String>
pub async fn css_value(&self, name: impl IntoArcStr) -> WebDriverResult<String>
Get the specified CSS property.
§Example:
let elem = driver.find(By::Id("my-element-id")).await?;
let css_color = elem.css_value("color").await?;
assert_eq!(css_color, "rgba(0, 0, 0, 1)");
// If an invalid CSS property is specified, a blank string is returned.
assert_eq!(elem.css_value("invalid-css-property").await?, "");Sourcepub async fn get_css_property(
&self,
name: impl IntoArcStr,
) -> WebDriverResult<String>
👎Deprecated since 0.30.0: This method has been renamed to css_value()
pub async fn get_css_property( &self, name: impl IntoArcStr, ) -> WebDriverResult<String>
Get the specified CSS property.
Sourcepub async fn is_selected(&self) -> WebDriverResult<bool>
pub async fn is_selected(&self) -> WebDriverResult<bool>
Return true if the WebElement is currently selected, otherwise false.
Sourcepub async fn is_displayed(&self) -> WebDriverResult<bool>
pub async fn is_displayed(&self) -> WebDriverResult<bool>
Return true if the WebElement is currently displayed, otherwise false.
§Example
let elem = driver.find(By::Id("button1")).await?;
assert!(elem.is_displayed().await?);Sourcepub async fn is_enabled(&self) -> WebDriverResult<bool>
pub async fn is_enabled(&self) -> WebDriverResult<bool>
Return true if the WebElement is currently enabled, otherwise false.
§Example
let elem = driver.find(By::Id("button1")).await?;
assert!(elem.is_enabled().await?);Sourcepub async fn is_clickable(&self) -> WebDriverResult<bool>
pub async fn is_clickable(&self) -> WebDriverResult<bool>
Return true if the WebElement is currently clickable (visible and enabled), otherwise false.
§Example
let elem = driver.find(By::Id("button1")).await?;
assert!(elem.is_clickable().await?);Sourcepub async fn is_present(&self) -> WebDriverResult<bool>
pub async fn is_present(&self) -> WebDriverResult<bool>
Return true if the WebElement is currently (still) present and not stale.
NOTE: This method simply queries the tag name to determine whether the element is still present.
IMPORTANT: If an element is re-rendered, it may be considered stale even though to the user it looks like it is still there.
The recommended way to check for the presence of an element is to simply search for the element again.
§Example
let elem = driver.find(By::Id("button1")).await?;
assert!(elem.is_present().await?);Sourcepub async fn find(&self, by: By) -> WebDriverResult<WebElement>
pub async fn find(&self, by: By) -> WebDriverResult<WebElement>
Search for a child element of this WebElement using the specified selector.
NOTE: For more powerful element queries including polling and filters, see the
WebElement::query method instead.
§Example:
let elem = driver.find(By::Id("my-element-id")).await?;
let child_elem = elem.find(By::Tag("button")).await?;Sourcepub async fn find_element(&self, by: By) -> WebDriverResult<WebElement>
👎Deprecated since 0.30.0: This method has been renamed to find()
pub async fn find_element(&self, by: By) -> WebDriverResult<WebElement>
Search for a child element of this WebElement using the specified selector.
Sourcepub async fn find_all(&self, by: By) -> WebDriverResult<Vec<WebElement>>
pub async fn find_all(&self, by: By) -> WebDriverResult<Vec<WebElement>>
Search for all child elements of this WebElement that match the specified selector.
NOTE: For more powerful element queries including polling and filters, see the
WebElement::query method instead.
§Example:
let elem = driver.find(By::Id("my-element-id")).await?;
let child_elems = elem.find_all(By::Tag("button")).await?;
for child_elem in child_elems {
assert_eq!(child_elem.tag_name().await?, "button");
}Sourcepub async fn find_elements(&self, by: By) -> WebDriverResult<Vec<WebElement>>
👎Deprecated since 0.30.0: This method has been renamed to find_all()
pub async fn find_elements(&self, by: By) -> WebDriverResult<Vec<WebElement>>
Search for all child elements of this WebElement that match the specified selector.
Sourcepub async fn send_keys(&self, key: impl Into<TypingData>) -> WebDriverResult<()>
pub async fn send_keys(&self, key: impl Into<TypingData>) -> WebDriverResult<()>
Send the specified input.
§Example:
let elem = driver.find(By::Name("input1")).await?;
elem.send_keys("thirtyfour").await?;You can also send special key combinations like this:
block_on(async {
let elem = driver.find(By::Name("input1")).await?;
elem.send_keys("selenium").await?;
elem.send_keys(Key::Control + "a").await?;
elem.send_keys("thirtyfour" + Key::Enter).await?;Sourcepub async fn screenshot_as_png_base64(&self) -> WebDriverResult<String>
pub async fn screenshot_as_png_base64(&self) -> WebDriverResult<String>
Take a screenshot of this WebElement and return it as PNG, base64 encoded.
Sourcepub async fn screenshot_as_png(&self) -> WebDriverResult<Vec<u8>>
pub async fn screenshot_as_png(&self) -> WebDriverResult<Vec<u8>>
Take a screenshot of this WebElement and return it as PNG bytes.
Sourcepub async fn screenshot(&self, path: &Path) -> WebDriverResult<()>
pub async fn screenshot(&self, path: &Path) -> WebDriverResult<()>
Take a screenshot of this WebElement and write it to the specified filename.
Sourcepub async fn focus(&self) -> WebDriverResult<()>
pub async fn focus(&self) -> WebDriverResult<()>
Focus this WebElement using JavaScript.
§Example:
let elem = driver.find(By::Name("input1")).await?;
elem.focus().await?;Sourcepub async fn scroll_into_view(&self) -> WebDriverResult<()>
pub async fn scroll_into_view(&self) -> WebDriverResult<()>
Scroll this element into view using JavaScript.
§Example:
let elem = driver.find(By::Id("button1")).await?;
elem.scroll_into_view().await?;Sourcepub async fn inner_html(&self) -> WebDriverResult<String>
pub async fn inner_html(&self) -> WebDriverResult<String>
Get the innerHtml property of this element.
§Example:
let elem = driver.find(By::Id("my-element-id")).await?;
let html = elem.inner_html().await?;Sourcepub async fn outer_html(&self) -> WebDriverResult<String>
pub async fn outer_html(&self) -> WebDriverResult<String>
Get the outerHtml property of this element.
§Example:
let elem = driver.find(By::Id("my-element-id")).await?;
let html = elem.outer_html().await?;Sourcepub async fn get_shadow_root(&self) -> WebDriverResult<WebElement>
pub async fn get_shadow_root(&self) -> WebDriverResult<WebElement>
Get the shadowRoot property of the current element.
Call this method on the element containing the #shadowRoot node.
You can then use the returned WebElement to query elements within the shadowRoot node.
Sourcepub async fn enter_frame(self) -> WebDriverResult<()>
pub async fn enter_frame(self) -> WebDriverResult<()>
Switch to the specified iframe element.
§Example:
let elem_iframe = driver.find(By::Id("iframeid1")).await?;
elem_iframe.enter_frame().await?;
// We can now search for elements within the iframe.
let elem = driver.find(By::Id("button1")).await?;
elem.click().await?;Sourcepub async fn js_drag_to(&self, target: &Self) -> WebDriverResult<()>
pub async fn js_drag_to(&self, target: &Self) -> WebDriverResult<()>
Drag the element to a target element using JavaScript.
§Example
let elem = driver.find(By::Id("draggable")).await?;
let target = driver.find(By::Id("target")).await?;
elem.js_drag_to(&target).await?;Sourcepub async fn parent(&self) -> WebDriverResult<Self>
pub async fn parent(&self) -> WebDriverResult<Self>
Get the parent of the WebElement.
§Example
let elem = driver.find(By::Id("child")).await?;
let parent = elem.parent().await?;Trait Implementations§
Source§impl Clone for WebElement
impl Clone for WebElement
Source§fn clone(&self) -> WebElement
fn clone(&self) -> WebElement
1.0.0 · Source§const fn clone_from(&mut self, source: &Self)
const fn clone_from(&mut self, source: &Self)
source. Read moreSource§impl Debug for WebElement
impl Debug for WebElement
Source§impl Display for WebElement
impl Display for WebElement
Source§impl ElementQueryable for WebElement
impl ElementQueryable for WebElement
Source§fn query(&self, by: By) -> ElementQuery
fn query(&self, by: By) -> ElementQuery
Return an ElementQuery instance for more executing powerful element queries.
This uses the builder pattern to construct queries that will return one or more elements, depending on the method specified at the end of the chain.
See ElementQuery for more documentation.
Source§impl ElementWaitable for WebElement
impl ElementWaitable for WebElement
Source§fn wait_until(&self) -> ElementWaiter
fn wait_until(&self) -> ElementWaiter
Return an ElementWaiter instance for more executing powerful explicit waits.
This uses the builder pattern to construct explicit waits using one of the provided predicates. Or you can provide your own custom predicate if desired.
See ElementWaiter for more documentation.
Source§impl PartialEq for WebElement
impl PartialEq for WebElement
Source§impl Serialize for WebElement
impl Serialize for WebElement
impl Eq for WebElement
Auto Trait Implementations§
impl Freeze for WebElement
impl !RefUnwindSafe for WebElement
impl Send for WebElement
impl Sync for WebElement
impl Unpin for WebElement
impl !UnwindSafe for WebElement
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<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
Source§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
Source§fn equivalent(&self, key: &K) -> bool
fn equivalent(&self, key: &K) -> bool
key and return true if they are equal.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> PolicyExt for Twhere
T: ?Sized,
impl<T> PolicyExt for Twhere
T: ?Sized,
Source§impl<T> ToStringFallible for Twhere
T: Display,
impl<T> ToStringFallible for Twhere
T: Display,
Source§fn try_to_string(&self) -> Result<String, TryReserveError>
fn try_to_string(&self) -> Result<String, TryReserveError>
ToString::to_string, but without panic on OOM.