selenium.webdriver.remote.webelement

Classes

BaseWebElement()

Abstract Base Class for WebElement.

WebElement(parent, id_)

Represents a DOM element.

class selenium.webdriver.remote.webelement.BaseWebElement[source]

Abstract Base Class for WebElement.

ABC’s will allow custom types to be registered as a WebElement to pass type checks.

class selenium.webdriver.remote.webelement.WebElement(parent, id_)[source]

Represents a DOM element.

Generally, all interesting operations that interact with a document will be performed through this interface.

All method calls will do a freshness check to ensure that the element reference is still valid. This essentially determines whether the element is still attached to the DOM. If this test fails, then an StaleElementReferenceException is thrown, and all future calls to this instance will fail.

property session_id: str
property tag_name: str

This element’s tagName property.

Returns:

The tag name of the element.

Example:

element = driver.find_element(By.ID, “foo”)

property text: str

The text of the element.

Returns:

The text of the element.

Example:

element = driver.find_element(By.ID, “foo”) print(element.text)

click() None[source]

Clicks the element.

Example:

element = driver.find_element(By.ID, “foo”) element.click()

submit() None[source]

Submits a form.

Example:

form = driver.find_element(By.NAME, “login”) form.submit()

clear() None[source]

Clears the text if it’s a text entry element.

Example:

text_field = driver.find_element(By.NAME, “username”) text_field.clear()

get_property(name) str | bool | WebElement | dict[source]

Gets the given property of the element.

Args:

name: Name of the property to retrieve.

Returns:

The value of the property.

Example:

text_length = target_element.get_property(“text_length”)

get_dom_attribute(name) str[source]

Get the HTML attribute value (not reflected properties) of the element.

Returns only attributes declared in the element’s HTML markup, unlike selenium.webdriver.remote.BaseWebElement.get_attribute.

Args:

name: Name of the attribute to retrieve.

Returns:

The value of the attribute.

Example:

text_length = target_element.get_dom_attribute(“class”)

get_attribute(name) str | None[source]

Gets the given attribute or property of the element.

This method will first try to return the value of a property with the given name. If a property with that name doesn’t exist, it returns the value of the attribute with the same name. If there’s no attribute with that name, None is returned.

Values which are considered truthy, that is equals “true” or “false”, are returned as booleans. All other non-None values are returned as strings. For attributes or properties which do not exist, None is returned.

To obtain the exact value of the attribute or property, use get_dom_attribute() or get_property() methods respectively.

Args:

name: Name of the attribute/property to retrieve.

Returns:

The value of the attribute/property.

Example:

# Check if the “active” CSS class is applied to an element. is_active = “active” in target_element.get_attribute(“class”)

is_selected() bool[source]

Returns whether the element is selected.

This method is generally used on checkboxes, options in a select and radio buttons.

Example:

is_selected = element.is_selected()

is_enabled() bool[source]

Returns whether the element is enabled.

Example:

is_enabled = element.is_enabled()

send_keys(*value: str) None[source]

Simulates typing into the element.

Use this to send simple key events or to fill out form fields. This can also be used to set file inputs.

Args:
value: A string for typing, or setting form fields. For setting

file inputs, this could be a local file path.

Examples:

To send a simple key event:

form_textfield = driver.find_element(By.NAME, “username”) form_textfield.send_keys(“admin”)

or to set a file input field:

file_input = driver.find_element(By.NAME, “profilePic”) file_input.send_keys(“path/to/profilepic.gif”) # Generally it’s better to wrap the file path in one of the methods # in os.path to return the actual path to support cross OS testing. # file_input.send_keys(os.path.abspath(“path/to/profilepic.gif”))

property shadow_root: ShadowRoot

Get the shadow root attached to this element if present (Chromium, Firefox, Safari).

Returns:

The ShadowRoot object.

Raises:

NoSuchShadowRoot: If no shadow root was attached to element.

Example:
try:

shadow_root = element.shadow_root

except NoSuchShadowRoot:

print(“No shadow root attached to element”)

is_displayed() bool[source]

Whether the element is visible to a user.

Example:

is_displayed = element.is_displayed()

property location_once_scrolled_into_view: dict

Get the element’s location on screen after scrolling it into view.

This may change without warning and scrolls the element into view before calculating coordinates for clicking purposes.

Returns:

The top lefthand corner location on the screen, or zero coordinates if the element is not visible.

Example:

loc = element.location_once_scrolled_into_view

property size: dict

Get the size of the element.

Returns:

The width and height of the element.

Example:

size = element.size

value_of_css_property(property_name) str[source]

Get the value of a CSS property.

Args:

property_name: The name of the CSS property to get the value of.

Returns:

The value of the CSS property.

Example:

value = element.value_of_css_property(“color”)

property location: dict

Get the location of the element in the renderable canvas.

Returns:

The x and y coordinates of the element.

Example:

loc = element.location

property rect: dict

Get the size and location of the element.

Returns:

A dictionary with size and location of the element.

Example:

rect = element.rect

property aria_role: str

Get the ARIA role of the current web element.

Returns:

The ARIA role of the element.

Example:

role = element.aria_role

property accessible_name: str

Get the ARIA Level of the current webelement.

Returns:

The ARIA Level of the element.

Example:

name = element.accessible_name

property screenshot_as_base64: str

Get a base64-encoded screenshot of the current element.

Returns:

The screenshot of the element as a base64 encoded string.

Example:

img_b64 = element.screenshot_as_base64

property screenshot_as_png: bytes

Get the screenshot of the current element as a binary data.

Returns:

The screenshot of the element as binary data.

Example:

element_png = element.screenshot_as_png

screenshot(filename) bool[source]

Save a PNG screenshot of the current element to a file.

Use full paths in your filename.

Args:
filename: The full path you wish to save your screenshot to. This

should end with a .png extension.

Returns:

True if the screenshot was saved successfully, False otherwise.

Example:

element.screenshot(“/Screenshots/foo.png”)

property parent

Get the WebDriver instance this element was found from.

Example:

element = driver.find_element(By.ID, “foo”) parent_element = element.parent

property id: str

Get the ID used by selenium.

This is mainly for internal use. Simple use cases such as checking if 2 webelements refer to the same element, can be done using ==:

Example:
if element1 == element2:

print(“These 2 are equal”)

find_element(by='id', value=None) WebElement[source]

Find an element given a By strategy and locator.

Args:
by: The locating strategy to use. Default is By.ID. Supported values include:
  • By.ID: Locate by element ID.

  • By.NAME: Locate by the name attribute.

  • By.XPATH: Locate by an XPath expression.

  • By.CSS_SELECTOR: Locate by a CSS selector.

  • By.CLASS_NAME: Locate by the class attribute.

  • By.TAG_NAME: Locate by the tag name (e.g., “input”, “button”).

  • By.LINK_TEXT: Locate a link element by its exact text.

  • By.PARTIAL_LINK_TEXT: Locate a link element by partial text match.

  • RelativeBy: Locate elements relative to a specified root element.

value: The locator value to use with the specified by strategy.

Returns:

The first matching WebElement found on the page.

Example:

element = driver.find_element(By.ID, “foo”)

find_elements(by='id', value=None) list[WebElement][source]

Find elements given a By strategy and locator.

Args:
by: The locating strategy to use. Default is By.ID. Supported values include:
  • By.ID: Locate by element ID.

  • By.NAME: Locate by the name attribute.

  • By.XPATH: Locate by an XPath expression.

  • By.CSS_SELECTOR: Locate by a CSS selector.

  • By.CLASS_NAME: Locate by the class attribute.

  • By.TAG_NAME: Locate by the tag name (e.g., “input”, “button”).

  • By.LINK_TEXT: Locate a link element by its exact text.

  • By.PARTIAL_LINK_TEXT: Locate a link element by partial text match.

  • RelativeBy: Locate elements relative to a specified root element.

value: The locator value to use with the specified by strategy.

Returns:

List of WebElements matching locator strategy found on the page.

Example:

element = driver.find_elements(By.ID, “foo”)