• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

The Programming Expert

Solving All of Your Programming Headaches

  • HTML
  • JavaScript
  • jQuery
  • PHP
  • Python
  • SAS
  • Ruby
  • About
You are here: Home / Python / Using Selenium to Get Text from Element in Python

Using Selenium to Get Text from Element in Python

June 1, 2022 Leave a Comment

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("http://theprogrammingexpert.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("http://theprogrammingexpert.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.

Other Articles You'll Also Like:

  • 1.  Using Python to Add Items to Set
  • 2.  Euclidean Algorithm and Extended Euclidean Algorithm in Python
  • 3.  How to Read Excel File from AWS S3 Bucket Using Python
  • 4.  pandas head – Return First n Rows from DataFrame
  • 5.  PROC MIXED Equivalent in Python for Least Squared Means ANOVA
  • 6.  Python if else do nothing – Using pass Statement to Do Nothing in Python
  • 7.  How to Check If File is Empty with Python
  • 8.  Using Python to Create Empty DataFrame with pandas
  • 9.  How to Use Python to Remove Zeros from List
  • 10.  Create Empty List in Python

About The Programming Expert

The Programming Expert is a compilation of a programmer’s findings in the world of software development, website creation, and automation of processes.

Programming allows us to create amazing applications which make our work more efficient, repeatable and accurate.

At the end of the day, we want to be able to just push a button and let the code do it’s magic.

You can read more about us on our about page.

Reader Interactions

Leave a Reply Cancel reply

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

Primary Sidebar

About The Programming Expert

the programming expert main image

Welcome to The Programming Expert. We are a group of US-based programming professionals who have helped companies build, maintain, and improve everything from simple websites to large-scale projects.

We built The Programming Expert to help you solve your programming problems with useful coding methods and functions in various programming languages.

Search

Learn Coding from Experts on Udemy

Looking to boost your skills and learn how to become a programming expert?

Check out the links below to view Udemy courses for learning to program in the following languages:

Copyright © 2023 · The Programming Expert · About · Privacy Policy