How to get text with selenium web driver in python?


We can extract text of an element with a selenium webdriver. This is done with the help of a text method. It fetches the text in an element which can be later validated.

First we need to identify the element with the help of any locators. Suppose we want to get the text of an element in below page.

Example

Code Implementation.

from selenium import webdriver
driver = webdriver.Chrome (executable_path="C:\chromedriver.exe")
# maximize with maximize_window()
driver.maximize_window()
driver.get("https://www.tutorialspoint.com/index.htm")
# identify element
l=driver.find_element_by_css_selector("h4")
# get text and print
print("Text is: " + l.text)
driver.close()

Output

Updated on: 28-Aug-2020

6K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements