19

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?

Jcyrss
  • 1,513
  • 3
  • 19
  • 31
  • Possible duplicate of [Properties and Attributes in HTML](http://stackoverflow.com/questions/6003819/properties-and-attributes-in-html) – Mark Lapierre Apr 26 '17 at 17:06
  • They are not selenium-specific terms. The [answer](http://stackoverflow.com/a/6004028/604131) to the linked question explains the difference. – Mark Lapierre Apr 26 '17 at 17:08
  • 1
    The minimal documentation for [get_property()](https://www.selenium.dev/selenium/docs/api/py/webdriver_remote/selenium.webdriver.remote.webelement.html#selenium.webdriver.remote.webelement.WebElement.get_property) also using the bogus property `text_length` was the root of my confusion. Much clearer if they'd used `text_length = len(target_element.get_property("innerText"))`. – Zach Young Mar 28 '20 at 04:54

2 Answers2

21

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

neet_jn
  • 448
  • 2
  • 10
2

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.
Morvader
  • 2,317
  • 3
  • 31
  • 44
  • 5
    This doesn't actually explain the differencel. It says what the methods do, but not what results you can expect. For that you need to explain the difference between properties and attributes, which is already explained elsewhere. – Mark Lapierre Apr 26 '17 at 17:11
  • This answer is specific to Python (not to Selenium). For example, in `thirty_four` crate (= library) in Rust, `get_attribute()` can only retrieve static attributes whereas `get_property()` can only retrieve dynamic attributes. – ynn Jan 22 '22 at 12:49