- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Selenium and Python to find elements and text?
We can find elements and its text with Selenium webdriver. First of all we have to identify the element with the help of any of the locators like id, classname, css and so on. Then to obtain the text we have to take the help of the text method.
Syntax
s = driver.find_element_by_css_selector("h4").text
Here driver is the webdriver object. The method find_element_by_css_selector is used to identify the element with css locator type and the locator value is passed as an argument to the method. Finally the text method is used to obtain the text content of the element.
Let us look at the html of an element having a text content. The output shall be You are browsing the best resource for Online Education.
Example
Code Implementation.
from selenium import webdriver driver = webdriver.Chrome(executable_path="C:\chromedriver.exe" # implicit wait applied driver.implicitly_wait(0.5) driver.get("https://www.tutorialspoint.com/index.htm") # to identify element and obtain text s = driver.find_element_by_css_selector("h4").text print("The text is: " + s)
Output
- Related Articles
- Difference between find Element and find Elements in Selenium
- Find elements inside forms and iframe using Java and Selenium WebDriver.
- Get the text from multiple elements with the same class in Selenium for Python?
- Find and click element by title Python Selenium.
- How to identify elements based on text visible on page in Selenium?
- Find elements using Selenium WebDriver?
- How to get text with selenium web driver in python?
- How to read a text file in Selenium with python?
- How to write a text file in Selenium with python?
- How to use text() in xpath in Selenium with python?
- How to clear the text entered in Selenium with python?
- How to find an element using the “Link Text/Partial Link Text” in Selenium?
- Get text using selenium web driver in python?
- How to extract text from a web page using Selenium and save it as a text file?
- How do I find an element that contains specific text in Selenium WebDriver (Python)?

Advertisements