thirtyfour

Struct WebElement

Source
pub struct WebElement {
    pub handle: SessionHandle,
    /* private fields */
}
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§

§handle: SessionHandle

Implementations§

Source§

impl WebElement

Source

pub fn from_json(value: Value, handle: 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) -> ElementRef

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>

Alias for WebElement::rect(), for compatibility with fantoccini.

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: &str) -> 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: &str) -> WebDriverResult<Option<String>>

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

pub async fn attr(&self, name: &str) -> 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: &str) -> WebDriverResult<Option<String>>

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

pub async fn css_value(&self, name: &str) -> 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: &str) -> WebDriverResult<String>

👎Deprecated since 0.30.0: This method has been renamed to css_value()
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 in order 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: impl Into<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()
Source

pub async fn find_all( &self, by: impl Into<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()
Source

pub async fn send_keys(&self, keys: impl AsRef<str>) -> 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".to_string()).await?;
elem.send_keys("thirtyfour" + Key::Enter).await?;
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?;

Trait Implementations§

Source§

impl Clone for WebElement

Source§

fn clone(&self) -> WebElement

Returns a copy 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 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, dst: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dst. 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> 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, 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