How to Find Element by Tag Name in Selenium
In Selenium, you can find an element by its tag name using the
find_element(By.TAG_NAME, "tagname") method. This method locates the first element matching the given tag name on the web page.Syntax
The syntax to find an element by tag name in Selenium is:
driver.find_element(By.TAG_NAME, "tagname")- Finds the first element with the specified tag name.driver.find_elements(By.TAG_NAME, "tagname")- Finds all elements with the specified tag name and returns a list.
Here, driver is your WebDriver instance, By.TAG_NAME is the locator strategy, and "tagname" is the HTML tag you want to find, like "input" or "button".
python
from selenium import webdriver from selenium.webdriver.common.by import By # Initialize WebDriver (example with Chrome) driver = webdriver.Chrome() driver.get("https://example.com") # Find first element by tag name first_input = driver.find_element(By.TAG_NAME, "input") # Find all elements by tag name all_buttons = driver.find_elements(By.TAG_NAME, "button")
Example
This example opens a webpage, finds the first h1 tag, and prints its text content. It also finds all p tags and prints how many paragraphs are found.
python
from selenium import webdriver from selenium.webdriver.common.by import By # Setup WebDriver with webdriver.Chrome() as driver: driver.get("https://www.example.com") # Find first h1 element heading = driver.find_element(By.TAG_NAME, "h1") print("Heading text:", heading.text) # Find all paragraph elements paragraphs = driver.find_elements(By.TAG_NAME, "p") print("Number of paragraphs found:", len(paragraphs))
Output
Heading text: Example Domain
Number of paragraphs found: 2
Common Pitfalls
- Using deprecated syntax: Older Selenium versions used
find_element_by_tag_name, which is now deprecated. Always usefind_element(By.TAG_NAME, "tagname"). - Expecting multiple elements but using
find_element:find_elementreturns only the first match. Usefind_elementsto get all matching elements. - Tag name case sensitivity: Tag names are case-insensitive in HTML, but always use lowercase strings for consistency.
- Element not found errors: If no element matches the tag name, Selenium throws
NoSuchElementException. Handle exceptions or check element presence before accessing.
python
from selenium.common.exceptions import NoSuchElementException from selenium.webdriver.common.by import By try: element = driver.find_element(By.TAG_NAME, "nonexistenttag") except NoSuchElementException: print("Element with given tag name not found.")
Output
Element with given tag name not found.
Quick Reference
| Method | Description | Returns |
|---|---|---|
| find_element(By.TAG_NAME, "tagname") | Finds first element with given tag name | WebElement |
| find_elements(By.TAG_NAME, "tagname") | Finds all elements with given tag name | List of WebElements |
| Deprecated: find_element_by_tag_name("tagname") | Older syntax, avoid using | WebElement |
Key Takeaways
Use find_element(By.TAG_NAME, "tagname") to get the first matching element by tag name.
Use find_elements(By.TAG_NAME, "tagname") to get all matching elements as a list.
Avoid deprecated methods like find_element_by_tag_name; use the By locator strategy instead.
Handle NoSuchElementException when elements are not found to avoid test failures.
Tag names are case-insensitive but use lowercase strings for consistency.