Introduction to Selenium
Selenium WebDriver Basics
WebDriver Commands
Synchronization in Selenium
Working with Different Browsers
Setting up WebDriver for different browsers
Handling Advanced User Interactions
Page Object Model (POM)
Introduction to POM
TestNG Framework
Creating and Running TestNG Tests
In Selenium, locating web elements by tag name is a common method used to interact with HTML elements. This approach can be particularly useful when you’re dealing with collections of similar elements, like rows in a table, paragraphs in an article, or items in a list.

Understanding 'find_element_by_tag_name'

Syntax:

				
					element = driver.find_element_by_tag_name('tag_name')
elements = driver.find_elements_by_tag_name('tag_name')

				
			
  • find_element_by_tag_name('tag_name')‘ – This finds the first element with the given tag name.
  • find_elements_by_tag_name('tag_name')' – This finds all elements with the given tag name and returns a list of elements.

Example:

Suppose you have the following HTML structure:

				
					<!DOCTYPE html>
<html>
<head>
    <title>Sample Page</title>
</head>
<body>
    <h1>Welcome to Sample Page</h1>
    <p>This is a sample paragraph.</p>
    <p>This is another sample paragraph.</p>
    <div>
        <a href="https://example.com" target="_blank" rel="noopener">Example Link</a>
    </div>
</body>
</html>

				
			

Here’s how you can use Selenium to interact with these elements by their tag name.

Step-by-Step Example

1. Set up Selenium:

Make sure you have the Selenium package installed:

				
					pip install selenium

				
			

Also, download and set up the appropriate WebDriver for your browser (e.g., ChromeDriver for Google Chrome).

2. Write the Selenium Script:

				
					from selenium import webdriver

# Set up the driver (make sure the driver executable is in your PATH or specify the path)
driver = webdriver.Chrome(executable_path='/path/to/chromedriver')

# Navigate to the webpage
driver.get('file:///path/to/your/sample.html')  # use a local file or a URL

# Find the first <h1> element
h1_element = driver.find_element_by_tag_name('h1')
print(f'h1 Element: {h1_element.text}')  # Output: Welcome to Sample Page

# Find all <p> elements
p_elements = driver.find_elements_by_tag_name('p')
for i, p in enumerate(p_elements, start=1):
    print(f'p Element {i}: {p.text}')
    # Output: p Element 1: This is a sample paragraph.
    #         p Element 2: This is another sample paragraph.

# Find the <a> element
a_element = driver.find_element_by_tag_name('a')
print(f'a Element href: {a_element.get_attribute("href")}')  # Output: https://example.com

# Always close the browser window
driver.quit()

				
			

Explanation:

  1. Setting up the driver:

    • webdriver.Chrome(executable_path='/path/to/chromedriver')‘: Initializes the Chrome WebDriver.
    • driver.get('file:///path/to/your/sample.html')‘: Navigates to the specified URL or local file.
  2. Finding elements by tag name:

    • find_element_by_tag_name('h1')‘: Finds the first ‘<h1>‘ element on the page.
    • find_elements_by_tag_name('p')‘: Finds all ‘<p>‘ elements on the page and stores them in a list.
    • find_element_by_tag_name('a')‘: Finds the first ‘<a>‘ element on the page.
  3. Interacting with elements:

    • element.text‘: Retrieves the text content of an element.
    • element.get_attribute('href')‘: Retrieves the value of the ‘href‘ attribute of an ‘<a>‘ element.
  4. Closing the browser:

driver.quit()‘: Closes the browser window and ends the WebDriver session.

Use Cases

Extracting Data from Tables

If you need to extract data from a table, you might use ‘find_elements_by_tag_name‘ to find all rows (‘<tr>‘) and cells (‘<td>‘):

				
					# Assuming a table structure like:
# <table>
#   <tr><td>Row 1, Cell 1</td><td>Row 1, Cell 2</td></tr>
#   <tr><td>Row 2, Cell 1</td><td>Row 2, Cell 2</td></tr>
# </table>

rows = driver.find_elements_by_tag_name('tr')
for row in rows:
    cells = row.find_elements_by_tag_name('td')
    row_data = [cell.text for cell in cells]
    print(row_data)

				
			

Interacting with Lists

To interact with list items in an unordered list (‘<ul>‘):

				
					# Assuming a list structure like:
# <ul>
#   <li>Item 1</li>
#   <li>Item 2</li>
#   <li>Item 3</li>
# </ul>

items = driver.find_elements_by_tag_name('li')
for item in items:
    print(item.text)

				
			

Locating elements by tag name in Selenium is straightforward and useful for interacting with groups of similar elements. By understanding the basic methods and practicing with examples, you can effectively use this technique to navigate and manipulate web pages in your test automation scripts.

Scroll to Top