To get the text from an HTML element when using Selenium in Python, you can use the attribute() function and accessing the ‘textContent’, ‘text’, or ‘innerText’ attributes.

from selenium import webdriver

driver = webdriver.Chrome()

driver.get("https://daztech.com/")

header = driver.find_element_by_css_selector("h1")

print(header.attribute('textContent'))

#Output:
The Programming Expert

The Selenium Python module gives you the tools you need to be able to automate many tasks when working with web browsers.

When working with web pages, the ability to get information about specific elements can be useful.

One such piece of information is if you want to get the text from an element.

To get the text from an HTML element when using Selenium in Python, you can use the attribute() function and accessing the ‘textContent’ or ‘innerText’ attributes.

from selenium import webdriver

driver = webdriver.Chrome()

driver.get("https://daztech.com/")

header = driver.find_element_by_css_selector("h1")

print(header.attribute('textContent'))

#Output:
The Programming Expert

Hopefully this article has been useful for you to understand how to get the text from an element in Python when using Selenium.

Categorized in:

Python,

Last Update: March 12, 2024