Struct WebElement

Source
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: ElementId

The element id.

§handle: Arc<SessionHandle>

The underlying session handle.

Implementations§

Source§

impl WebElement

Source

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.

Source

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.

Source

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.

Source

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?;
Source

pub async fn rectangle(&self) -> WebDriverResult<ElementRect>

👎Deprecated since 0.32.0: Use rect() instead

Alias for WebElement::rect().

Source

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");
Source

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?;
Source

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?;
Source

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");
Source

pub async fn value(&self) -> WebDriverResult<Option<String>>

Convenience method for getting the (optional) value property of this element.

Source

pub async fn click(&self) -> WebDriverResult<()>

Click the WebElement.

§Example:
let elem = driver.find(By::Id("button1")).await?;
elem.click().await?;
Source

pub async fn clear(&self) -> WebDriverResult<()>

Clear the WebElement contents.

§Example:
let elem = driver.find(By::Css("input[type='text']")).await?;
elem.clear().await?;
Source

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);
Source

pub async fn get_property( &self, name: impl IntoArcStr, ) -> WebDriverResult<Option<String>>

👎Deprecated since 0.30.0: This method has been renamed to prop()

Get the specified property.

Source

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);
Source

pub async fn get_attribute( &self, name: impl IntoArcStr, ) -> WebDriverResult<Option<String>>

👎Deprecated since 0.30.0: This method has been renamed to attr()

Get the specified attribute.

Source

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?, "");
Source

pub async fn get_css_property( &self, name: impl IntoArcStr, ) -> WebDriverResult<String>

👎Deprecated since 0.30.0: This method has been renamed to css_value()

Get the specified CSS property.

Source

pub async fn is_selected(&self) -> WebDriverResult<bool>

Return true if the WebElement is currently selected, otherwise false.

Source

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?);
Source

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?);
Source

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?);
Source

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?);
Source

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?;
Source

pub async fn find_element(&self, by: By) -> WebDriverResult<WebElement>

👎Deprecated since 0.30.0: This method has been renamed to find()

Search for a child element of this WebElement using the specified selector.

Source

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");
}
Source

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

👎Deprecated since 0.30.0: This method has been renamed to find_all()

Search for all child elements of this WebElement that match the specified selector.

Source

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?;
Source

pub async fn screenshot_as_png_base64(&self) -> WebDriverResult<String>

Take a screenshot of this WebElement and return it as PNG, base64 encoded.

Source

pub async fn screenshot_as_png(&self) -> WebDriverResult<Vec<u8>>

Take a screenshot of this WebElement and return it as PNG bytes.

Source

pub async fn screenshot(&self, path: &Path) -> WebDriverResult<()>

Take a screenshot of this WebElement and write it to the specified filename.

Source

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

Focus this WebElement using JavaScript.

§Example:
let elem = driver.find(By::Name("input1")).await?;
elem.focus().await?;
Source

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?;
Source

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?;
Source

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?;
Source

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.

Source

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?;
Source

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?;
Source

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

Source§

fn clone(&self) -> WebElement

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

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

Performs copy-assignment from source. Read more
Source§

impl Debug for WebElement

Source§

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

Formats the value using the given formatter. Read more
Source§

impl Display for WebElement

Source§

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

Formats the value using the given formatter. Read more
Source§

impl ElementQueryable for WebElement

Source§

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

Source§

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

Source§

fn eq(&self, other: &Self) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl Serialize for WebElement

Source§

fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where S: Serializer,

Serialize this value into the given Serde serializer. Read more
Source§

impl Eq for WebElement

Auto Trait Implementations§

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<Q, K> Equivalent<K> for Q
where Q: Eq + ?Sized, K: Borrow<Q> + ?Sized,

Source§

fn equivalent(&self, key: &K) -> bool

Checks if this value is equivalent to the given key. Read more
Source§

impl<Q, K> Equivalent<K> for Q
where Q: Eq + ?Sized, K: Borrow<Q> + ?Sized,

Source§

fn equivalent(&self, key: &K) -> bool

Compare self to key and return true if they are equal.
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> PolicyExt for T
where T: ?Sized,

Source§

fn and<P, B, E>(self, other: P) -> And<T, P>
where T: Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow only if self and other return Action::Follow. Read more
Source§

fn or<P, B, E>(self, other: P) -> Or<T, P>
where T: Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow if either self or other returns Action::Follow. Read more
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> ToString for T
where T: Display + ?Sized,

Source§

fn to_string(&self) -> String

Converts the given value to a String. Read more
Source§

impl<T> ToStringFallible for T
where T: Display,

Source§

fn try_to_string(&self) -> Result<String, TryReserveError>

ToString::to_string, but without panic on OOM.

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

impl<T> ErasedDestructor for T
where T: 'static,

Source§

impl<T> Resolve for T
where T: Resolve,