Introduction
The Name Locator is one of the basic locator strategies in Selenium WebDriver. It identifies a web element using the value of its name attribute in the HTML document.
Many web applications assign meaningful name attributes to form elements such as text boxes, password fields, radio buttons, checkboxes, and dropdowns. When an element does not have a unique ID, the Name Locator is often the next best choice.
Although the name attribute is not always unique like an id, it is widely used in web forms and provides a simple and readable way to locate elements.
In this tutorial, you will learn what the Name Locator is, why it is used, its syntax, practical examples, real-world applications, common mistakes, best practices, and frequently asked interview questions.
What is a Name Locator?
The Name Locator is a Selenium locator strategy that identifies a web element using the value of its HTML name attribute.
For example, consider the following HTML element:
<input type="password" name="password">
The element can be located using:
driver.find_element(By.NAME, "password")
Selenium searches for the element whose name attribute matches the specified value.
Why Do We Use the Name Locator?
The Name Locator is commonly used because it:
Is simple and easy to understand.
Works well with HTML forms.
Is often available when an ID is missing.
Makes automation scripts more readable.
Is suitable for locating input fields and form controls.
Syntax
driver.find_element(By.NAME, "name_value")
Where:
By.NAME specifies that Selenium should locate the element using its
nameattribute.name_value is the value of the HTML
nameattribute.
Practical Example
The following example demonstrates how to locate the password input field using its name attribute.
The automation script opens the Selenium practice login page, locates the password field using the Name Locator, and verifies that the correct element has been found.
from selenium import webdriver
from selenium.webdriver.common.by import By
# Topic: 11. Basic Locators - Name
# Practice site: https://the-internet.herokuapp.com/login
#
# By.NAME locates an element using its name attribute value.
def test_find_element_by_name():
driver = webdriver.Chrome()
try:
driver.get("https://the-internet.herokuapp.com/login")
password = driver.find_element(By.NAME, "password")
assert password.get_attribute("name") == "password"
finally:
driver.quit()
Output
Browser launched successfully.
Login page opened successfully.
Password field located using Name.
Element Name:
password
Assertion Passed.
Test Executed Successfully.
Understanding the Code
Import Required Modules
from selenium import webdriver
from selenium.webdriver.common.by import By
Imports the Selenium WebDriver module and the By class required for locating web elements.
Launch Chrome Browser
driver = webdriver.Chrome()
Starts a new Chrome browser session.
Open the Login Page
driver.get("https://the-internet.herokuapp.com/login")
Navigates to the Selenium practice login page.
Locate the Password Field Using Name
password = driver.find_element(By.NAME, "password")
Locates the password input field using its HTML name attribute.
Validate the Located Element
assert password.get_attribute("name") == "password"
Verifies that the located element has the expected name attribute.
If the assertion passes, Selenium has successfully located the correct element.
Close the Browser
driver.quit()
Closes all browser windows and ends the WebDriver session.
Execution Flow
Launch Browser
│
▼
Open Login Page
│
▼
Locate Element Using Name
│
▼
Verify Name Attribute
│
▼
Assertion Passes
│
▼
Close Browser
Automation Testing Example
Suppose you’re automating the login page of an employee management system.
The password field has the following HTML:
<input type="password" name="password">
The element can be located using:
driver.find_element(By.NAME, "password")
This provides a simple and readable way to identify the password field.
Real-World Example
Consider an online registration form where the email input field is defined as:
<input type="email" name="email">
You can locate the field using:
driver.find_element(By.NAME, "email")
This approach works well when the element does not have a unique ID.
Common Mistakes Beginners Make
Using an Incorrect Name Value
Incorrect
driver.find_element(By.NAME, "pass")
If the actual name is "password", Selenium throws a NoSuchElementException.
Correct
driver.find_element(By.NAME, "password")
Confusing Name with ID
Incorrect
driver.find_element(By.NAME, "username")
when the element only has:
<input id="username">
Always inspect the HTML before selecting a locator.
Assuming the Name Attribute is Always Unique
Unlike IDs, multiple elements may share the same name attribute.
Always verify that the element is uniquely identified.
Best Practices
Use the Name Locator when a unique ID is unavailable.
Verify that the
nameattribute uniquely identifies the element.Prefer ID over Name whenever possible.
Inspect the HTML before selecting a locator.
Avoid using Name if multiple elements share the same value.
Conclusion
The Name Locator is a simple and effective way to identify web elements using their HTML name attribute. It is commonly used for locating form elements such as text boxes and password fields.
Although it is not always as reliable as the ID locator, it remains one of the most commonly used locator strategies in Selenium. When used correctly, it helps create clean, readable, and maintainable automation scripts.
Frequently Asked Questions (FAQs)
What is the Name Locator in Selenium?
The Name Locator identifies a web element using its HTML name attribute.
What is the syntax for the Name Locator?
driver.find_element(By.NAME, "name_value")
Is the Name Locator better than the ID Locator?
No.
The ID Locator is generally preferred because IDs are usually unique. The Name Locator is commonly used when a unique ID is unavailable.
Can multiple elements have the same name?
Yes.
Unlike IDs, multiple elements can share the same name attribute.
When should I use the Name Locator?
Use it when the element has a stable and preferably unique name attribute and no suitable ID is available.
Key Takeaways
The Name Locator identifies elements using their HTML
nameattribute.The syntax is
driver.find_element(By.NAME, "name_value").It is commonly used for locating form elements such as text boxes and password fields.
Prefer the ID Locator over the Name Locator whenever possible.
Verify that the
nameattribute uniquely identifies the element.The Name Locator is simple, readable, and widely used in Selenium automation.
Understanding the Name Locator is an essential Selenium interview topic and a fundamental automation skill.
