Name Locator in Selenium: A Complete Guide (Advantages, Limitations, Syntax, Examples, and Alternatives)

 Selenium WebDriver supports many types of element locators like ID, Name, TagName, Class, XPath, and CSS Selectors to find and interact with the web element on a page. One of them is the Name locator. You can use the By.name() method to identify the element using the name attribute of the element. This guide will help you to learn how to use the Name Locator in Selenium, its advantages, limitations, and best practices.

What is Name Locator in Selenium?

It is an element locating strategy in Selenium WebDriver. It is used to find elements based on the value of their name attribute. If Selenium finds multiple elements with the same name, it will select the first matching element by default.

Syntax: WebElement element = driver.findElement(By.name("element_name"));

Why Use Name Locator?

There are a couple of advantages and disadvantages of using it in Selenium WebDriver. Let's look at both of them one by one.

Advantages:

  • Simple and Readable: It is easy to use in Selenium test script. Also, it helps to improve code readability as a name often describes the function or purpose of the element.
  • Faster Execution: Searching an element by Name attribute is faster than XPath element locator.
  • Works Well in All Major Browsers: No browser compatibility issues.
  • Stable: Generally, it is stable as its value does not change like the ID attribute.

Limitations:

  • Not Unique Always: There are chances of multiple elements having the same name attribute value. In that case, Selenium will pick the first occurrence.
  • Dynamic Changes: If the value of the name attribute changes frequently, Locator will become unreliable.
  • Limited Coverage: You may not find the Name attribute for all elements. In such cases, you need to use other locators like find element by ID, Class, write XPath, or CSS.

Is the Name Locator Faster than Other Element Locators?

Answer is Yes and No both. It is slower than ID locator, but it is faster than XPath and CSS Selector. Also, it depends on the web page structure. It performs better on the well-structured page.

Why it is Faster Than XPath and CSS Selectors?

Here are a few reasons why we can say it is faster.
  • Direct Attribute Access: It is faster than complex XPath or CSS queries because the Name locator (By.name("elementName")) can directly access the element’s name attribute.
  • Index-Based Searching: Generally, browsers optimize search for the name attribute. So it is possible to quickly look up a specific attribute instead of traversing the entire DOM.

When is Name Locator Slower?

Here are the scenarios when it is slower.
  • If multiple elements have the same name attribute, Selenium may have to filter through them. That makes it slower than the ID locator.
  • If the name attribute is not unique, Selenium will return a list of elements. It requires additional filtering.
  • In many modern web applications, I have seen that elements are generated dynamically. In such cases, the name attribute may be missing for the element.
In summary, it is slower than ID locator, but it is faster than className, CSS Selector, and XPath locators.

For better performance, you should use ID locator (If it is present and stable) instead of Name. But if ID is not present, then name selector is a better choice compared to other locators like CSS and XPath.

How to get the name attribute of an element?

To get the name attribute of an element, I am using two methods.
  1. Right click on element and select Inspect element option. It will open the HTML structure of the element in a window. You can copy the name attribute from this window.
  2. I am using SelectorsHub element locator. Right click on element, navigate to SelectorsHub option and click Copy Name.

Syntax of Name Locator

Syntax in most popular programming languages like Java, Python, and C# is given below.

Syntax in Java:

WebElement element = driver.findElement(By.name("element_name"));

Syntax in Python:

element = driver.find_element(By.NAME, "element_name")

Syntax in C#:

IWebElement element = driver.FindElement(By.Name("element_name"));

Examples of How to Use Name Locator in Selenium

Now let us look at some real-world and practical examples of using this locator in Selenium.

Example 1: Locate an Input Field by Name

Consider a form with an input field for a user's name. Here's how you can identify an input element by name and type value in it.

HTML Code

<input type="text" name="username" />
Selenium Code (Java):
WebElement usernameField = driver.findElement(By.name("username"));
usernameField.sendKeys("TestUserName");

Example 2: Click a Button by Name

Locate a button using Name locator and click on it.

HTML Code:

<button type="submit" name="submitButton">Submit</button>

Selenium Code (Python):

submit_button = driver.find_element(By.NAME, "submitButton")
submit_button.click()

Also, it is possible to write XPath using the name attribute.

Here is an example for the submit button.

//button[@name='submitButton']

Example 3: Select a Radio Button by Name

You can use the Name locator to select a specific option from radio buttons.
<input type="radio" name="gender" value="male" /> Male
<input type="radio" name="gender" value="female" /> Female

Selenium Code (C#):

IWebElement maleOption = driver.FindElement(By.Name("gender"));
maleOption.Click();

How to Handle Multiple Elements with Same Name?

If multiple elements have the same name, and it is only a choice, then you can use the findElements() method and select the element based on index.

Here is an example of how to click on the second element when multiple elements are found with the same name attributes.

List<WebElement> radioButtons = driver.findElements(By.name("age"));
radioButtons.get(1).click(); // Clicks the second radio button

Best Practices

You should follow these guidelines when using the Name selector.
  • Ensure Uniqueness: Make sure the Name attribute you are using is unique on the page.
  • Combine with Other Locators: To make it robust and if it is necessary, combine it with other locators like CSS Selectors or XPath.
  • Avoid Hardcoding: If the value of Name is changing frequently, it is advisable to use a dynamic strategy.
  • Don’t Rely Exclusively on Name: In some cases, the ID or class attribute might be better for more accuracy. 

Alternative Locators in Selenium

If you find Name Locator isn't reliable, you can consider using the following locators instead.
  • ID Locator – By.id("element_id")
  • Class Name Locator – By.className("class_name")
  • CSS Selector – By.cssSelector("css_expression")
  • XPath – By.xpath("xpath_expression")
Name is one of the powerful element locators in Selenium, especially when dealing with forms and input elements. It is simple, stable, and easy to use in Selenium test scripts. However, like other locators, it has some limitations and disadvantages. If you have proper knowledge of when to and how to use it, you can write better automation tests using it. No matter if you are new to Selenium or a seasoned professional.

No comments:

Post a Comment