get_attribute() method in Selenium Python is used to get the value of a given property or attribute. This post will discuss the get_attribute() method in detail.
- Method declaration – def get_attribute(self, name) -> str | None
- What does it do – It accepts an attribute or property as its parameter and returns the value of that given attribute or property.
get_attribute() first attempts to return the value of a property with the specified name that is passed as its argument. If the property does not exist, the function returns the value of an attribute with the same name. If neither a property nor an attribute with the given name can be found, the function returns ‘None’.
Attribute vs Property
Attributes provide additional information about the elements, and all tags can have attributes that provide more information about them.
For example, we have the “type” and “id” attributes on the <input> tag.
<input type="text" id = "firstName">
Here type and id are the attribute keys, whereas text and firstName are their values, respectively.
When the browser parses the HTML, it converts the HTML attributes to DOM properties.
So, for input tag –
<input type="text" id = "firstName">
type and text are the attributes.
When the browser parses the HTML, it will create a DOM property of the attributes, which can be read using input.type or input.id.
Please note that all HTML attributes are not converted to properties with the same name. E.g., class attribute is converted to the className property.
In a nutshell, we can say that an attribute is related to HTML, whereas a property is associated with DOM.
Code Example
Let’s now try to find the value of the “name” attribute highlighted in the image below. This element is present on our selenium playground website ( https://testkru.com/Elements/TextFields ).
We can use the element’s id to find the element using the find_element() method in Selenium Python.
element = driver.find_element(By.ID, 'firstName')
Then, we can use the get_attribute() method to find the value of the “name” attribute.
element.get_attribute("name")
Whole Code
from selenium import webdriver
from selenium.webdriver.common.by import By
driver = webdriver.Chrome()
driver.get("https://testkru.com/Elements/TextFields")
element = driver.find_element(By.ID, 'firstName')
print("Atrribute's Value: " + element.get_attribute("name"))
Output –
Atrribute's Value: firstName
Special case of boolean attributes
get_attribute() method would return true or None for HTML’s boolean attributes. Below is the list of such boolean attributes –
async, autofocus, autoplay, checked, compact, complete, controls, declare, defaultchecked, defaultselected, defer, disabled, draggable, ended, formnovalidate, hidden, indeterminate, iscontenteditable, ismap, itemscope, loop, multiple, muted, nohref, noresize, noshade, novalidate, nowrap, open, paused, pubdate, readonly, required, reversed, scoped, seamless, seeking, selected, truespeed, willvalidate.
Let’s take the example of the element highlighted in the image below. This element is present at – https://testkru.com/Elements/Checkboxes.
With what we have discussed so far, get_attribute(“checked”) should have returned the “checked” value. But it would return “true” because “checked” is a boolean attribute.
from selenium import webdriver
from selenium.webdriver.common.by import By
driver = webdriver.Chrome()
driver.get("https://testkru.com/Elements/Checkboxes")
element = driver.find_element(By.ID, 'firstSelect5')
print("Atrribute's Value: " + element.get_attribute("checked"))
Output –
Atrribute's Value: true
Getting the value of properties using get_attribute() method
As we discussed previously, the get_attribute() method also tries to retrieve the value of the property. So, let’s see it with an example.
We will retrieve the value of the “className” property for the highlighted element. Since “className” is not an attribute, get_attribute(“className”) will retrieve the property instead.
from selenium import webdriver
from selenium.webdriver.common.by import By
driver = webdriver.Chrome()
driver.get("https://testkru.com/Elements/TextFields")
element = driver.find_element(By.ID, 'firstName')
print("Atrribute's Value: " + element.get_attribute("className"))
Output –
Atrribute's Value: pt-1 pb-1 pr-2 pl-2
Note: The get_attribute() method attempts to retrieve the value of a property first. If the property doesn’t exist, then only it tries to retrieve the value of an attribute with the same name.
Getting the value of an unknown property or attribute
If a property or attribute with the given name does not exist, the get_attribute() method returns None. For example, if we try to fetch the value of the property or attribute named “codekru“, then the method will return None.
from selenium import webdriver
from selenium.webdriver.common.by import By
driver = webdriver.Chrome()
driver.get("https://testkru.com/Elements/TextFields")
element = driver.find_element(By.ID, 'firstName')
attribute_value = element.get_attribute("codekru")
if attribute_value is None:
print("Attribute's value is None")
else:
print("Attribute's value: " + attribute_value)
Output –
Attribute's value is None
We hope that you have liked the article. If you have any doubts or concerns, please feel free to write us in the comments or mail us at admin@codekru.com.