Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

what is the difference between 'property' and 'attribute' in Selenium WebElement?

Tags:

selenium

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?

like image 484
Jcyrss Avatar asked Apr 26 '17 07:04

Jcyrss


People also ask

What is the difference between property and attribute?

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.

What are attributes in Selenium?

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.

What's the difference between an attribute and a property JS?

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.

What is the difference between WebElement and locator?

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.


2 Answers

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

like image 120
neet_jn Avatar answered Oct 09 '22 21:10

neet_jn


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.
like image 45
Morvader Avatar answered Oct 09 '22 21:10

Morvader