MCPcopy Create free account
hub / github.com/SeleniumHQ/selenium / get_attribute

Method get_attribute

py/selenium/webdriver/remote/webelement.py:187–219  ·  view source on GitHub ↗

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)

Source from the content-addressed store, hash-verified

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.

Callers 15

select_by_indexMethod · 0.80
deselect_by_indexMethod · 0.80
_predicateFunction · 0.80
test_move_and_clickFunction · 0.80
test_context_clickFunction · 0.80
test_double_clickFunction · 0.80
test_double_clickFunction · 0.80
test_context_clickFunction · 0.80
test_move_and_clickFunction · 0.80

Calls 2

_load_jsFunction · 0.85
execute_scriptMethod · 0.45