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 t
(self, name: str)
| 185 | return self._execute(Command.GET_ELEMENT_ATTRIBUTE, {"name": name})["value"] |
| 186 | |
| 187 | def get_attribute(self, name: str) -> str | None: |
| 188 | """Gets the given attribute or property of the element. |
| 189 | |
| 190 | This method will first try to return the value of a property with the |
| 191 | given name. If a property with that name doesn't exist, it returns the |
| 192 | value of the attribute with the same name. If there's no attribute with |
| 193 | that name, ``None`` is returned. |
| 194 | |
| 195 | Values which are considered truthy, that is equals "true" or "false", |
| 196 | are returned as booleans. All other non-``None`` values are returned |
| 197 | as strings. For attributes or properties which do not exist, ``None`` |
| 198 | is returned. |
| 199 | |
| 200 | To obtain the exact value of the attribute or property, |
| 201 | use :func:`~selenium.webdriver.remote.BaseWebElement.get_dom_attribute` or |
| 202 | :func:`~selenium.webdriver.remote.BaseWebElement.get_property` methods respectively. |
| 203 | |
| 204 | Args: |
| 205 | name: Name of the attribute/property to retrieve. |
| 206 | |
| 207 | Returns: |
| 208 | The value of the attribute/property. |
| 209 | |
| 210 | Example: |
| 211 | # Check if the "active" CSS class is applied to an element. |
| 212 | is_active = "active" in target_element.get_attribute("class") |
| 213 | """ |
| 214 | if getAttribute_js is None: |
| 215 | _load_js() |
| 216 | attribute_value = self.parent.execute_script( |
| 217 | f"/* getAttribute */return ({getAttribute_js}).apply(null, arguments);", self, name |
| 218 | ) |
| 219 | return attribute_value |
| 220 | |
| 221 | def is_selected(self) -> bool: |
| 222 | """Returns whether the element is selected. |