• 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
  • VBA
  • 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 Split a Number into Digits
  • 2.  Read Last Line of File Using Python
  • 3.  How to Create Array from 1 to n in Python
  • 4.  PROC MEANS Equivalent in Python
  • 5.  Add Tuple to List in Python
  • 6.  Writing Multiple Lines Lambda Expression in Python
  • 7.  Python acosh – Find Hyperbolic Arccosine of Number Using math.acosh()
  • 8.  How to Check if String Contains Uppercase Letters in Python
  • 9.  Changing Python Turtle Size with turtlesize() Function
  • 10.  Sort List of Tuples 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

The Programming Expert is a compilation of hundreds of code snippets to help you find solutions to your problems in Python, JavaScript, PHP, HTML, SAS, and more.

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 © 2022 · The Programming Expert · About · Privacy Policy