How to get text of a tag in selenium – Python?
Last Updated :
21 Mar, 2024
Selenium is a powerful tool for controlling web browsers through programs and performing browser automation. It is functional for all browsers, works on all major OS and its scripts are written in various languages i.e Python, Java, C#, etc, we will be working with Python.
In this article, we will write a Python Script for getting the text from the tag name using selenium module.
Step-by-step Approach
Step #1: Import required libraries
Python3
from selenium import webdriver
|
Step #2: Create a Chrome object or specify web driver path if it is not present in the default path.
Python3
driver = webdriver.Chrome( 'Enter Chrome Path' )
driver.get( 'Enter Web URL' )
|
Step #3: Specify the tag name, which you want to extract the text.
Syntax:
Object Name.find_element_by_tag_name(Tag Name)
Python3
print (element.text)
driver.close()
|
Below is the implementation.
Python3
from selenium import webdriver
driver = webdriver.Chrome()
element = driver.find_element_by_tag_name( 'a' )
print (element.text)
driver.close()
|
Output:
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...