Selenium WebElement has 2 methods, in Python, they are 'get_attribute' and 'get_property' . The documentation is very simple and unclear to me.
What is the difference of them on earth?
Property used when value doesn't change very often (usually used at startup or for environment variable) Attributes is a value (object child) of an Element (object) which can change very often/all the time and be or not persistent.
Selenium's getAttribute() method is used to get attributes of HTML elements under test. This method is a part of the WebElement Class. The getAttribute() method can be used for getting values for different attributes like class, name,src, CSS style value, etc.
In JavaScript (the DOM, really), an element has attributes and properties. The terms are often used interchangeably, but they're actually two separate things. An attribute is the initial state when rendered in the DOM. A property is the current state.
They are different. A locator is nothing more than an abstract way of defining how an element will be found. A WebElement is just the reference to that element within the DOM. It is Selenium 's way of representing a DOM element to you so that you can manipulate it.
An attribute is a static attribute of a given DOM node, as where a property is a computed property of the DOM node object. An example of a property would be the checked
state of a checkbox, or value
or an input field. As where an attribute would be href
of an anchor tag or the type
of an input DOM.
<a href="https://google.com" id="hello">Hello World</a>
<input type="checkbox" id="foo" checked>
<input type="text" id="bar" value="cheesecake">
link_location = document.querySelector('#hello').getAttribute('href')
// # href="https://google.com"
input_checkbox = document.querySelector('#foo').getAttribute('type')
// # type="checkbox"
checkbox_checked = document.querySelector('#foo').checked
// # computed property of the DOM node
textbox_value = document.querySelector('#bar').value
// # computed property of the DOM node
https://www.w3schools.com/jsref/dom_obj_all.asp
Seems that get_attribute
search for properties and then attributes and get_property
just for properties.
From code documentation
get_property
def get_property(self, name):
"""
Gets the given property of the element.
:Args:
- name - Name of the property to retrieve.
get_attribute
def get_attribute(self, name):
"""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.
:Args:
- name - Name of the attribute/property to retrieve.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With