tag_name

Introduction

The tag_name property is one of Selenium’s WebElement properties used to retrieve the HTML tag name of a web element. It allows automation engineers to identify the type of element Selenium has located and verify that the correct element is being used during test execution.

The tag_name property is particularly useful when validating dynamically generated elements, debugging locator issues, and performing assertions within automation frameworks. Whether you’re verifying that an element is an input, button, a, div, or textarea element, tag_name provides an easy and reliable way to identify its HTML type.

In this tutorial, you’ll learn what tag_name is, why it is used, its syntax, practical examples, real-world use cases, common mistakes, and best practices.


What is tag_name?

The tag_name property returns the HTML tag name of a web element in lowercase.

Selenium retrieves the element’s HTML tag directly from the DOM after locating it successfully.

For example, consider the following HTML:

<input id="username">

<button>Login</button>

<textarea></textarea>

<a href="/login">
    Login
</a>

Selenium returns:

input
button
textarea
a

using:

element.tag_name

The tag_name property is commonly used for retrieving:

  • input

  • button

  • textarea

  • a

  • div

  • span

  • select

  • img

  • form

  • table


Why Use tag_name?

The tag_name property is useful because it:

  • Identifies the HTML type of an element.

  • Improves automation reliability.

  • Supports UI validation.

  • Helps debug locator-related issues.

  • Produces readable automation scripts.

  • Is widely used in professional automation frameworks.


Syntax

element.tag_name

Where:

  • element → Previously located WebElement.

  • tag_name → Returns the element’s HTML tag name in lowercase.


Example

The Selenium practice website contains a Username textbox and a Login button. Selenium retrieves their HTML tag names and validates them using assertions.

The Selenium code is:

from selenium import webdriver
from selenium.webdriver.common.by import By


# Topic: 17. Retrieving Element Information - tag_name
# Practice site: https://the-internet.herokuapp.com/login
# Run: pytest -s 17_examples/test_06_tag_name.py
#
# tag_name returns the HTML tag name of the element in lowercase.


def test_tag_name():
    driver = webdriver.Chrome()

    try:
        driver.get(
            "https://the-internet.herokuapp.com/login"
        )

        username = driver.find_element(
            By.ID,
            "username"
        )

        submit = driver.find_element(
            By.CSS_SELECTOR,
            "button[type='submit']"
        )

        assert username.tag_name == "input"
        assert submit.tag_name == "button"

    finally:
        driver.quit()

Output

The Username textbox and
Login button are identified
successfully and their
HTML tag names are
validated correctly.

Understanding the Code

Import the By Class

from selenium.webdriver.common.by import By

Imports Selenium’s locator strategies.

Open the Login Page

driver.get(
    "https://the-internet.herokuapp.com/login"
)

Launches the Selenium practice website.

Locate the Username Field

driver.find_element(
    By.ID,
    "username"
)

Locates the Username textbox displayed on the webpage.

Locate the Login Button

driver.find_element(
    By.CSS_SELECTOR,
    "button[type='submit']"
)

Locates the Login button.

Retrieve the Tag Names

username.tag_name

submit.tag_name

Returns:

input

button

which represent the HTML tag names of the respective elements.

Validate the Result

assert username.tag_name == (
    "input"
)

assert submit.tag_name == (
    "button"
)

Verifies that Selenium successfully retrieves the expected tag names.


How tag_name Works

            Python Script
                   │
                   ▼
            Locate Web Element
                   │
                   ▼
               tag_name
                   │
                   ▼
           Read HTML Tag Name
                   │
                   ▼
            Retrieve Value
                   │
                   ▼
             Compare Values
                   │
                   ▼
             Perform Validation

Practical Example

Suppose you’re automating a Registration page.

The application contains:

  • Textboxes

  • Dropdown menus

  • Buttons

  • Textareas

Before performing an action, Selenium can verify the element type using:

element.tag_name

For example:

input

confirms that the located element is a textbox before calling:

send_keys()

This significantly improves automation reliability.


Automation Testing Example

Consider an online banking application.

Automation engineers frequently validate:

  • Login buttons.

  • Input fields.

  • Search boxes.

  • Dropdown menus.

  • Navigation links.

  • Forms.

  • Tables.

  • Dynamic UI components.

Professional automation frameworks use tag_name during debugging and validation to ensure that Selenium interacts with the correct element types.


Real-World Example

Automation engineers frequently use tag_name while automating:

  • Banking applications.

  • E-Commerce websites.

  • Healthcare portals.

  • CRM systems.

  • ERP applications.

  • SaaS products.

  • Enterprise web applications.

  • Registration forms.

  • Dynamic dashboards.

Tag name validation is particularly useful when working with dynamically generated webpages and reusable framework components.


Advantages of tag_name

  • Retrieves HTML tag names efficiently.

  • Improves automation reliability.

  • Supports UI validation.

  • Helps debug locator-related issues.

  • Produces maintainable automation scripts.

  • Works across browsers.


Common HTML Tag Names

Some frequently used HTML tags include:

input
button
textarea
a
select
option
div
span
img
table
form
label

These tags are commonly encountered during Selenium automation testing.


Common Mistakes Beginners Make

Confusing tag_name with get_attribute()

Incorrect

element.get_attribute(
    "tag_name"
)

Correct

element.tag_name

The tag_name property retrieves the HTML tag name directly and should not be confused with HTML attributes.


Ignoring Element Validation

Avoid

element.send_keys(
    "admin"
)

without verifying whether the element is actually an:

input

or:

textarea

element.

Using tag_name helps improve framework reliability during dynamic element handling.


Assuming Tag Names are Uppercase

Selenium always returns:

input
button
textarea

in lowercase.

Avoid writing assertions such as:

assert element.tag_name == (
    "INPUT"
)

because they will fail.


Best Practices

  • Use tag_name when validating element types.

  • Verify tag names before performing critical operations whenever appropriate.

  • Keep assertions simple and readable.

  • Use tag_name during framework debugging.

  • Combine tag_name validation with other WebElement methods whenever necessary.

  • Use synchronization techniques for dynamically loaded webpages.


Conclusion

The tag_name property provides a simple and reliable mechanism for identifying the HTML type of web elements during Selenium automation testing. It plays an important role in UI validation, framework debugging, and dynamic element handling across modern web applications.

Understanding how and when to use tag_name correctly helps automation engineers build scalable, reliable, and maintainable Selenium automation frameworks.


Frequently Asked Questions (FAQs)

What is tag_name in Selenium?

The tag_name property returns the HTML tag name of a web element in lowercase.

What is the syntax of tag_name?

element.tag_name

Which values can tag_name return?

Common values include:

  • input

  • button

  • textarea

  • div

  • a

  • select

  • span

  • img

  • form

Does tag_name return uppercase values?

No.

Selenium always returns HTML tag names in lowercase.

When should I use tag_name?

Use it when:

  • Validating element types.

  • Debugging locator issues.

  • Working with reusable framework components.

  • Handling dynamically generated web elements.


Key Takeaways

  • tag_name returns the HTML tag name of a web element in lowercase.

  • It is useful for validating element types during automation testing.

  • Common values include input, button, textarea, and div.

  • Use tag_name while debugging locators and handling dynamic webpages.

  • Keep assertions simple and readable.

  • The tag_name property is widely used in professional Selenium automation frameworks.