Monday, September 29, 2025
HomeLanguagesHow to get text of a tag in selenium – Python?

How to get text of a tag in selenium – Python?

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. We will be getting the tag text from this URL.

Step-by-step Approach

Step #1: Import required libraries

Python3




# Import Library
from selenium import webdriver


Step #2: Create a Chrome object or specify web driver path if it is not present in the default path and assign URL.

Python3




# Chrome Path
driver = webdriver.Chrome('Enter Chrome Path')
  
# Web URL
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




# Get Text
print (element.text)
  
# Close the window
driver.close()


Below is the implementation.

Python3




# Import Library
from selenium import webdriver
  
# Chrome Path
driver = webdriver.Chrome()
  
# Web URL
  
element = driver.find_element_by_tag_name('a')
  
# Get Text
print(element.text)
  
# Close the window
driver.close()


Output:

Dominic
Dominichttp://wardslaus.com
infosec,malicious & dos attacks generator, boot rom exploit philanthropist , wild hacker , game developer,
RELATED ARTICLES

Most Popular

Dominic
32324 POSTS0 COMMENTS
Milvus
84 POSTS0 COMMENTS
Nango Kala
6695 POSTS0 COMMENTS
Nicole Veronica
11860 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11918 POSTS0 COMMENTS
Shaida Kate Naidoo
6807 POSTS0 COMMENTS
Ted Musemwa
7073 POSTS0 COMMENTS
Thapelo Manthata
6763 POSTS0 COMMENTS
Umr Jansen
6771 POSTS0 COMMENTS