How to get text content of element in Selenium Python?

Selenium Python – Get text content of an element

In this tutorial, you will learn how to get the text content of an element, using Selenium in Python.

To get the text content of an element, i.e., the visible text of an element in the webpage, in Selenium Python, locate the required element, and read the text attribute of the element object. The text attribute returns a string value representing the content in the element.

element.text

Examples

1. Get text content of an element

In this example, we shall consider loading the HTML file at path https://pythonexamples.org/tmp/selenium/index-13.html . The contents of this HTML file is given below.

index.html

<html>
 <body>
  <h2>Hello User!</h2>
  <div id="parent">
    <div id="child1">This is child 1.</div>
    <div id="child2">This is child 2.</div>
    <div id="child3">This is child 3.</div>
  </div>
 </body>
</html>
Copy

In the following program, we initialize a driver, then load the URL, find the element with id="child2", and get its text content using text attribute.

Python Program

from selenium import webdriver
from selenium.webdriver.chrome.service import Service as ChromeService
from webdriver_manager.chrome import ChromeDriverManager
from selenium.webdriver.common.by import By

# Setup chrome driver
driver = webdriver.Chrome(service=ChromeService(ChromeDriverManager().install()))

# Navigate to the url
driver.get('https://pythonexamples.org/tmp/selenium/index-13.html')

# Find an element by its ID
element = driver.find_element(By.ID, 'child2')

# Get text content of element
text_content = element.text
print(text_content)

# Close the driver
driver.quit()
Copy

Output

Selenium Python - Get text content of an element

2. Get text content of an element with children

In the following program, we initialize a driver, then load the URL, the same one we used in the previous example. Find the element with id="parent", and get its text content using text attribute.

The element with id="parent" has three children elements. The text of all the visible children is returned.

Python Program

from selenium import webdriver
from selenium.webdriver.chrome.service import Service as ChromeService
from webdriver_manager.chrome import ChromeDriverManager
from selenium.webdriver.common.by import By

# Setup chrome driver
driver = webdriver.Chrome(service=ChromeService(ChromeDriverManager().install()))

# Navigate to the url
driver.get('https://pythonexamples.org/tmp/selenium/index-13.html')

# Find an element by its ID
element = driver.find_element(By.ID, 'parent')

# Get text content of element
text_content = element.text
print(text_content)

# Close the driver
driver.quit()
Copy

Output

Python Selenium - Get text content of an element with children

3. Get text content of an element with some of its children invisible

In this example, we shall consider loading the HTML file at path https://pythonexamples.org/tmp/selenium/index-14.html . The contents of this HTML file is given below.

<html>
 <body>
  <h2>Hello User!</h2>
  <div id="parent">
    <div id="child1">This is child 1.</div>
    <div id="child2">This is child 2.</div>
    <div id="child3" hidden>This is child 3.</div>
  </div>
 </body>
</html>
Copy

If we try to get the text content of div with id="parent", only the visible content is returned. Since the div with id="child3" is hidden, the respective text is not returned.

Python Program

from selenium import webdriver
from selenium.webdriver.chrome.service import Service as ChromeService
from webdriver_manager.chrome import ChromeDriverManager
from selenium.webdriver.common.by import By

# Setup chrome driver
driver = webdriver.Chrome(service=ChromeService(ChromeDriverManager().install()))

# Navigate to the url
driver.get('https://pythonexamples.org/tmp/selenium/index-14.html')

# Find an element by its ID
element = driver.find_element(By.ID, 'parent')

# Get text content of element
text_content = element.text
print(text_content)

# Close the driver
driver.quit()
Copy

Output

Python Selenium - Get text content of an element with some of its children invisible

Summary

In this Python Selenium tutorial, we have given instructions on how to get the text content of an element, with example programs.

Related Tutorials

Quiz on Selenium

Q1. What is Selenium Python used for?

Not answered

Q2. What is the method used to interact with web elements in Selenium Python?

Not answered

Q3. Which of the following is not a method to wait for a web element to load in Selenium Python?

Not answered

Q4. What is the method used to navigate back to the previous page in Selenium Python?

Not answered

Q5. Which of the following is not a commonly used web element locator in Selenium Python?

Not answered
Code copied to clipboard successfully 👍