How to find Element by Tag Name using Selenium?

Find Element by Tag Name

To find an HTML Element by tag name using Selenium in Python, call find_element() method and pass By.TAG_NAME as the first argument, and the tag name (of the HTML Element we need to find) as the second argument.

find_element(By.TAG_NAME, "tag_name_value")

If there are multiple HTML Elements with the same given tag name, then find_element() returns the first HTML Element of those.

1. There is only one element by given tag name

In the following example, we have an HTML page with a div element. In the Python program, we will find the HTML element whose tag name is 'div', using find_element() method, and print that element to the console.

HTML Webpage

<html>
 <body>
  <p>This is a sample paragraph.</p>
  <div>Hello World!</div>
 </body>
</html>

Python Program (Selenium)

from selenium import webdriver
from webdriver_manager.chrome import ChromeDriverManager
from selenium.webdriver.chrome.service import Service as ChromeService
from selenium.webdriver.common.by import By

# Setup chrome driver
driver = webdriver.Chrome(service=ChromeService(ChromeDriverManager().install()))

# Navigate to the url
driver.get('https://pythonexamples.org/tmp/selenium/index-46.html')

# Find element by Tag Name
my_div = driver.find_element(By.TAG_NAME, 'div')
print(my_div.get_attribute("outerHTML"))

# Close the driver
driver.quit()

Output

<div>Hello World!</div>

2. There are multiple elements by given tag name

Now, let us consider the scenario where there are multiple HTML Elements with the given tag name. We will use find_element() function to find the first element whose tag name is 'div'.

Even through there are two elements with the same given tag name, find_element() returns only the first element of those.

index.html

<html>
 <body>
  <p>This is a sample paragraph.</p>
  <div>Hello World!</div>
  <div>Welcome.</div>
 </body>
</html>

Python Program (Selenium)

from selenium import webdriver
from webdriver_manager.chrome import ChromeDriverManager
from selenium.webdriver.chrome.service import Service as ChromeService
from selenium.webdriver.common.by import By

# Setup chrome driver
driver = webdriver.Chrome(service=ChromeService(ChromeDriverManager().install()))

# Navigate to the url
driver.get('https://pythonexamples.org/tmp/selenium/index-47.html')

# Find element by Tag Name
my_div = driver.find_element(By.TAG_NAME, 'div')
print(my_div.get_attribute("outerHTML"))

# Close the driver
driver.quit()

Output

<div>Hello World!</div>

3. There is no element by given tag name

If there is no element by given tag name, find_element() function raises NoSuchElementException.

In the following example, we have taken an HTML page with no div elements. When we try to find element by the tag name 'div', using find_element() function, the function throws NoSuchElementException.

HTML Webpage

<html>
 <body>
  <p>This is a sample paragraph.</p>
 </body>
</html>

Python Program (Selenium)

from selenium import webdriver
from webdriver_manager.chrome import ChromeDriverManager
from selenium.webdriver.chrome.service import Service as ChromeService
from selenium.webdriver.common.by import By

# Setup chrome driver
driver = webdriver.Chrome(service=ChromeService(ChromeDriverManager().install()))

# Navigate to the url
driver.get('https://pythonexamples.org/tmp/selenium/index-48.html')

# Find element by Tag Name
my_div = driver.find_element(By.TAG_NAME, 'div')
print(my_div.get_attribute("outerHTML"))

# Close the driver
driver.quit()

Output

    raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"tag name","selector":"div"}

Summary

In this Python Selenium tutorial, we learned how to find an element by tag name in webpage, using Selenium.

Related Tutorials

Privacy Policy Terms of Use

SitemapContact Us