Selenium Locators -Locating By Name.
Last Updated :
27 Jun, 2025
Finding web elements accurately is a fundamental skill for successful automation testing with Selenium WebDriver. In this article, we will see how to use the Name locator in Selenium and how to implement it with a practical, working example using a Google search test.
Understanding Name Locator
Locators are essentially the precise instructions we give WebDriver to find and interact with specific components on a webpage. The Name locator targets elements based on their HTML name attribute, which is commonly used in form elements like input fields, radio buttons, or dropdowns.
Name LocatorExample of an HTML input element showcasing the name
attribute:
<input type="text" id="username" name="username" placeholder="Enter your username">
Example of Name Locator
Here is an example of using the Name Locator to automate a Google search using Selenium WebDriver in Java.
Step 1: Set up Your Project
To get started with Selenium WebDriver, make sure you have the following setup:
- Java Development Kit (JDK) installed on your system to compile and run Java applications.
- Selenium WebDriver added to your project dependencies, which allows you to interact with web elements and automate browsers.
- Maven or Gradle for managing your project's dependencies, including Selenium WebDriver, to ensure easy and efficient integration.
If using Maven, your pom.xml
should include the following dependency:
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-java</artifactId>
<version>3.141.59</version>
</dependency>
Step 2: Sample Code for Google Search Using the Name Locator
Java
package test; // Ensure this package matches your project structure
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
public class GoogleSearchByNameTest { // Renamed the class for clarity
public static void main(String[] args) {
// Step 1: Set the path to Chromedriver manually
// Replace the path below with the actual path where you have downloaded chromedriver.exe
System.setProperty("webdriver.chrome.driver", "C:/path/to/chromedriver.exe"); // Update this path accordingly
// Step 2: Initialize a new ChromeDriver instance
// This command opens a new Chrome browser window.
WebDriver driver = new ChromeDriver();
try {
// Step 3: Open Google.com
System.out.println("Navigating to Google.com...");
driver.get("https://www.google.com");
System.out.println("Successfully navigated to: " + driver.getCurrentUrl());
// Step 4: Locate the search box using By.name("q")
// The 'q' is the common 'name' attribute for Google's search input field.
System.out.println("Locating search box using By.name(\"q\")...");
WebElement searchBox = driver.findElement(By.name("q"));
System.out.println("Search box located.");
// Step 5: Enter the search query "Selenium" into the search box
System.out.println("Typing 'Selenium' into the search box...");
searchBox.sendKeys("Selenium");
System.out.println("Text entered.");
// Step 6: Submit the search query
// Calling submit() on a text field within a form simulates pressing 'Enter'.
System.out.println("Submitting search query...");
searchBox.submit();
System.out.println("Search submitted.");
// Step 7: Print the title of the search results page
// This verifies that we've landed on the search results page.
// You might add an Assert here in a formal test.
String pageTitle = driver.getTitle();
System.out.println("Current Page Title: " + pageTitle);
} catch (Exception e) {
System.err.println("An error occurred during the test: " + e.getMessage());
e.printStackTrace(); // Print stack trace for debugging
} finally {
// Step 8: Close the browser
// driver.quit() closes all browser windows opened by the WebDriver instance.
System.out.println("Closing the browser...");
if (driver != null) {
driver.quit();
}
System.out.println("Browser closed. Test finished.");
}
}
}
Output:
Output of Locator "name"Name locator in Selenium is a straightforward and effective way to interact with web elements. It targets elements based on the "name" attribute in the HTML, making it especially useful for automating tasks such as filling out forms, search boxes, or login fields. The Name locator is not only simple to implement but also reliable, ensuring that the correct elements are targeted during automation.