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?
Syntax: WebElement element = driver.findElement(By.name("element_name"));
Why Use Name Locator?
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?
Why it is Faster Than XPath and CSS Selectors?
- 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?
- 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.
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?
- 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.
- I am using SelectorsHub element locator. Right click on element, navigate to SelectorsHub option and click Copy Name.
Syntax of Name Locator
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
HTML Code
<input type="text" name="username" />
WebElement usernameField = driver.findElement(By.name("username"));
usernameField.sendKeys("TestUserName");
Example 2: Click a Button by Name
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
<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?
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
- 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
- ID Locator – By.id("element_id")
- Class Name Locator – By.className("class_name")
- CSS Selector – By.cssSelector("css_expression")
- XPath – By.xpath("xpath_expression")
No comments:
Post a Comment