get_property() method in Selenium Python

Selenium has been the go-to tool for web automation for years, but even experienced testers sometimes get confused about when to use get_property() versus get_attribute(). If you’ve ever struggled with retrieving dynamic properties from web elements, get_property() is the method you need to understand, whereas we have covered get_attribute() in a different post.

This post will discuss the get_property() method in detail.

  • Method declaration – def get_property(self, name) -> str | bool | WebElement | dict:
  • What does it do? – It gets the value of the property passed in the argument.

Now, you might be wondering—what exactly is a “property”?

A property is a characteristic of a DOM (Document Object Model) element that represents its current state in memory. Properties are part of the JavaScript object model and can change dynamically as the page runs.

Okay, that gives us the definition, but how do you know which property’s value you need?

Here comes developer tools to the rescue.

Let’s look at the element below, where the text is hidden. Although it’s not visible on the page, the text still exists in the DOM.

hidden element

So, how can we navigate to the properties tab shown above?

  • Inspect any element in the browser.
  • Then click on the “Elements” tab at the top.
  • Select the element you want to inspect, and then navigate to the “Properties” tab.
  • Here, you’ll find all the properties associated with the element. The get_property() method in Selenium allows us to retrieve the values of any of these properties.

So, if we want to find hidden text, we can simply search for it within the Properties tab. This will show us which property contains the text, helping us determine the right one to use.

search any property

Now, let’s put this into action using the same element from the images above. Our goal is to retrieve the value of the hidden text. Since we already know that the "textContent" property holds the text, we’ll use it to extract the value.

The highlighted element in the images above is located on this page – https://testkru.com/Elements/TextMessages

We can use the element’s id to find the element using the find_element() method in Selenium Python.

1
element = driver.find_element(By.ID, 'hiddenText')

Then, we can use the get_property() method to find the value of the “textContent” property.

1
element.get_property("textContent")
1
2
3
4
5
6
7
8
9
10
11
12
13
14
from selenium import webdriver
from selenium.webdriver.common.by import By
 
options = webdriver.ChromeOptions()
options.add_experimental_option("detach", True)
driver = webdriver.Chrome(options)
 
element = driver.find_element(By.ID, 'hiddenText')
hiddenText = element.get_property("textContent")
 
print("Text: " + hiddenText)
 
driver.quit()

Output –

Text:  A Hidden Text

In this case, get_property() will return None, as demonstrated in the example below.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
from selenium import webdriver
from selenium.webdriver.common.by import By
 
options = webdriver.ChromeOptions()
options.add_experimental_option("detach", True)
driver = webdriver.Chrome(options)
 
element = driver.find_element(By.ID, 'hiddenText')
hiddenText = element.get_property("abc")
 
if hiddenText is None:
    print("None is returned")
 
driver.quit()

Output –

None is returned

This is it. 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.

Liked the article? Share this on

Leave a Comment

Your email address will not be published. Required fields are marked *