pub struct WebElement<'a> {
pub element_id: ElementId,
pub session: &'a WebDriverSession,
}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_element(By::Id("input-result"))?;You can also search for a child element of another element as follows:
let elem = driver.find_element(By::Css("div[data-section='section-buttons']"))?;
let child_elem = elem.find_element(By::Tag("button"))?;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§session: &'a WebDriverSessionImplementations§
Source§impl<'a> WebElement<'a>
impl<'a> WebElement<'a>
Sourcepub fn new(session: &'a WebDriverSession, element_id: ElementId) -> Self
pub fn new(session: &'a WebDriverSession, element_id: ElementId) -> Self
Create a new WebElement struct.
Typically you would not call this directly. WebElement structs are usually constructed by calling one of the find_element*() methods either on WebDriver or another WebElement.
Sourcepub fn rect(&self) -> WebDriverResult<ElementRect>
pub fn rect(&self) -> WebDriverResult<ElementRect>
Get the bounding rectangle for this WebElement.
Sourcepub fn tag_name(&self) -> WebDriverResult<String>
pub fn tag_name(&self) -> WebDriverResult<String>
Get the tag name for this WebElement.
§Example:
let elem = driver.find_element(By::Id("button1"))?;
assert_eq!(elem.tag_name()?, "button");Sourcepub fn class_name(&self) -> WebDriverResult<Option<String>>
pub fn class_name(&self) -> WebDriverResult<Option<String>>
Get the class name for this WebElement.
§Example:
let elem = driver.find_element(By::Id("button1"))?;
let class_name_option = elem.class_name()?; // Option<String>Sourcepub fn id(&self) -> WebDriverResult<Option<String>>
pub fn id(&self) -> WebDriverResult<Option<String>>
Get the id for this WebElement.
§Example:
let elem = driver.find_element(By::Id("button1"))?;
let id_option = elem.id()?; // Option<String>Sourcepub fn text(&self) -> WebDriverResult<String>
pub fn text(&self) -> WebDriverResult<String>
Get the text contents for this WebElement.
§Example:
let elem = driver.find_element(By::Id("button-result"))?;
let text = elem.text()?;Sourcepub fn value(&self) -> WebDriverResult<Option<String>>
pub fn value(&self) -> WebDriverResult<Option<String>>
Convenience method for getting the (optional) value attribute of this element.
Sourcepub fn click(&self) -> WebDriverResult<()>
pub fn click(&self) -> WebDriverResult<()>
Sourcepub fn clear(&self) -> WebDriverResult<()>
pub fn clear(&self) -> WebDriverResult<()>
Sourcepub fn get_property(&self, name: &str) -> WebDriverResult<Option<String>>
pub fn get_property(&self, name: &str) -> WebDriverResult<Option<String>>
Get the specified property.
§Example:
let bool_value_option = elem.get_property("checked")?; // Option<String>
assert_eq!(bool_value_option, Some("true".to_string()));
let string_value_option = elem.get_property("name")?; // Option<String>
assert_eq!(string_value_option, Some("input2".to_string()));Sourcepub fn get_attribute(&self, name: &str) -> WebDriverResult<Option<String>>
pub fn get_attribute(&self, name: &str) -> WebDriverResult<Option<String>>
Get the specified attribute.
§Example:
let attribute_option = elem.get_attribute("name")?; // Option<String>
assert_eq!(attribute_option, Some("input2".to_string()));Sourcepub fn get_css_property(&self, name: &str) -> WebDriverResult<String>
pub fn get_css_property(&self, name: &str) -> WebDriverResult<String>
Get the specified CSS property.
§Example:
let css_color = elem.get_css_property("color")?;
assert_eq!(css_color, r"rgba(0, 0, 0, 1)");Sourcepub fn is_selected(&self) -> WebDriverResult<bool>
pub fn is_selected(&self) -> WebDriverResult<bool>
Return true if the WebElement is currently selected, otherwise false.
Sourcepub fn is_displayed(&self) -> WebDriverResult<bool>
pub fn is_displayed(&self) -> WebDriverResult<bool>
Return true if the WebElement is currently displayed, otherwise false.
§Example
let displayed = elem.is_displayed()?;Sourcepub fn is_enabled(&self) -> WebDriverResult<bool>
pub fn is_enabled(&self) -> WebDriverResult<bool>
Return true if the WebElement is currently enabled, otherwise false.
§Example
let enabled = elem.is_enabled()?;Sourcepub fn is_clickable(&self) -> WebDriverResult<bool>
pub fn is_clickable(&self) -> WebDriverResult<bool>
Return true if the WebElement is currently clickable (visible and enabled), otherwise false.
§Example
let clickable = elem.is_clickable()?;Sourcepub fn is_present(&self) -> WebDriverResult<bool>
pub 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 present = elem.is_present()?;Sourcepub fn find_element(&self, by: By<'_>) -> WebDriverResult<WebElement<'_>>
pub fn find_element(&self, by: By<'_>) -> WebDriverResult<WebElement<'_>>
Search for a child element of this WebElement using the specified selector.
§Example:
let elem = driver.find_element(By::Css("div[data-section='section-buttons']"))?;
let child_elem = elem.find_element(By::Tag("button"))?;Sourcepub fn find_elements(&self, by: By<'_>) -> WebDriverResult<Vec<WebElement<'_>>>
pub fn find_elements(&self, by: By<'_>) -> WebDriverResult<Vec<WebElement<'_>>>
Search for all child elements of this WebElement that match the specified selector.
§Example:
let elem = driver.find_element(By::Css("div[data-section='section-buttons']"))?;
let child_elems = elem.find_elements(By::Tag("button"))?;
for child_elem in child_elems {
assert_eq!(child_elem.tag_name()?, "button");
}Sourcepub fn send_keys<S>(&self, keys: S) -> WebDriverResult<()>where
S: Into<TypingData>,
pub fn send_keys<S>(&self, keys: S) -> WebDriverResult<()>where
S: Into<TypingData>,
Send the specified input.
§Example:
You can specify anything that implements Into<TypingData>. This
includes &str and String.
elem.send_keys("selenium")?;You can also send special key combinations like this:
elem.send_keys("selenium")?;
elem.send_keys(Keys::Control + "a")?;
elem.send_keys(TypingData::from("thirtyfour") + Keys::Enter)?;Sourcepub fn screenshot_as_base64(&self) -> WebDriverResult<String>
pub fn screenshot_as_base64(&self) -> WebDriverResult<String>
Take a screenshot of this WebElement and return it as a base64-encoded String.
Sourcepub fn screenshot_as_png(&self) -> WebDriverResult<Vec<u8>>
pub fn screenshot_as_png(&self) -> WebDriverResult<Vec<u8>>
Take a screenshot of this WebElement and return it as PNG bytes.
Sourcepub fn screenshot(&self, path: &Path) -> WebDriverResult<()>
pub fn screenshot(&self, path: &Path) -> WebDriverResult<()>
Take a screenshot of this WebElement and write it to the specified filename.
Sourcepub fn focus(&self) -> WebDriverResult<()>
pub fn focus(&self) -> WebDriverResult<()>
Focus this WebElement using JavaScript.
§Example:
let elem = driver.find_element(By::Name("input1"))?;
elem.focus()?;Sourcepub fn scroll_into_view(&self) -> WebDriverResult<()>
pub fn scroll_into_view(&self) -> WebDriverResult<()>
Scroll this element into view using JavaScript.
§Example:
let elem = driver.find_element(By::Id("button1"))?;
elem.scroll_into_view()?;Sourcepub fn inner_html(&self) -> WebDriverResult<String>
pub fn inner_html(&self) -> WebDriverResult<String>
Get the innerHtml property of this element.
§Example:
let elem = driver.find_element(By::XPath(r##"//*[@id="button1"]/.."##))?;
let html = elem.inner_html()?;Sourcepub fn outer_html(&self) -> WebDriverResult<String>
pub fn outer_html(&self) -> WebDriverResult<String>
Get the outerHtml property of this element.
§Example:
let elem = driver.find_element(By::XPath(r##"//*[@id="button1"]/.."##))?;
let html = elem.outer_html()?;Trait Implementations§
Source§impl<'a> Clone for WebElement<'a>
impl<'a> Clone for WebElement<'a>
Source§fn clone(&self) -> WebElement<'a>
fn clone(&self) -> WebElement<'a>
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read moreSource§impl<'a> Debug for WebElement<'a>
impl<'a> Debug for WebElement<'a>
Source§impl<'a> Display for WebElement<'a>
impl<'a> Display for WebElement<'a>
Source§impl ElementQueryable for WebElement<'_>
impl ElementQueryable for WebElement<'_>
Source§fn query<'a>(&'a self, by: By<'a>) -> ElementQuery<'_>
fn query<'a>(&'a self, by: By<'a>) -> 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.