How to get the text of an element in Selenium Python?

This post will discuss various ways of getting the text of an element using Selenium and Python. Please note that here, we are talking about the visible text only, not the hidden text. ( Please read this post to get the hidden text of an element )

The visible text means the text that is visible on the webpage and not hidden using any CSS property.

<p>
 This is a text
</p>

Here, “This is a text” is visible on the webpage, and the same would also be returned by the methods explored in this post

<p style="visibility: hidden;">
 This is also a text
</p>

The text is not visible on the webpage; thus, the methods we will explore in this post are irrelevant.

Below are some of the ways to get the text of an element and its sub-elements –

Let’s look at them one by one.

Using text property

Selenium WebElement class provides us with a text property that returns the visible text of an element.

For example, we will try to get the highlighted element’s visible text in the image below. This element is present on our playground website ( https://testkru.com/Elements/TextMessages )

Text element

As we can see, the element’s id is “plainText“. We can use it to find the element using the find_element() method.

element = driver.find_element(By.ID, 'plainText')

Then, we can access the text property of the element to get the visible text.

print("Visible text: "+element.text)

Whole code

from selenium import webdriver
from selenium.webdriver.common.by import By

driver = webdriver.Chrome()
driver.get("https://testkru.com/Elements/TextMessages")

element = driver.find_element(By.ID, 'plainText')
print("Visible text: "+element.text)

Output –

Visible text: A Plain Text
Using get_attribute() method

The get_attribute() method in Selenium is used to fetch the value of a specified attribute or property that is provided as its argument. The innerText property is used to get the text content of an element. Therefore, we can use the get_attribute("innerText") to retrieve the text content value of an element.

Let’s use the same element and retrieve its text using the get_attribute() method.

from selenium import webdriver
from selenium.webdriver.common.by import By

driver = webdriver.Chrome()
driver.get("https://testkru.com/Elements/TextMessages")

element = driver.find_element(By.ID, 'plainText')
print("Visible text: "+element.get_attribute("innerText"))

Output –

Visible text: A Plain Text
Using execute_script() method

We can also use the execute_script() method in Selenium to retrieve the value of the innerText property. This method enables us to execute JavaScript code within Selenium. To retrieve the innerText property value of a specific element, we can use the following JavaScript code –

return arguments[0].innerText

where arguments[0] is the element whose text we want to find.

Whole code

from selenium import webdriver
from selenium.webdriver.common.by import By

driver = webdriver.Chrome()
driver.get("https://testkru.com/Elements/TextMessages")

element = driver.find_element(By.ID, 'plainText')
print(driver.execute_script("return arguments[0].innerText",element))

Output –

Visible text: A Plain Text

This is it. We hope that you like 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.

Related Articles –
Liked the article? Share this on

Leave a Comment

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