17

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,305
  • 1
  • 15
  • 26
  • 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
  • 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

3 Answers3

18

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
  • 418
  • 2
  • 10
1

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,277
  • 3
  • 30
  • 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
0

I would like to ask a question related to this topic.

To get the value of a subproperty of a property, for example in Python, how would you name it correctly?

That is to say for the example of the property classList, if in the detachable there was a subelement of key. How would we name this subelement to obtain its value?

->classlist

->key: 100

when i copy the element from google chrome, it looks like 'classlist.key', but i don't get any value

get_property('classlist.key')

  • As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Jun 03 '22 at 14:07