Locating Web Elements Using ID, Name, and Class Name in Selenium Java
In Selenium automation, one of the most fundamental tasks is locating web elements on a page to interact with them. Whether you're clicking a button, entering text in a form, or reading data, everything begins with identifying the correct element. Selenium WebDriver offers multiple strategies to locate elements, and among the most commonly used are ID, Name, and Class Name.
This blog will guide you through using these three strategies in Selenium Java, including best practices and real-world usage.
1. Locating Elements by ID
The ID attribute is the most preferred and fastest locator strategy in Selenium. It is unique to each element on a page, making it reliable and efficient.
Syntax:
java
WebElement element = driver.findElement(By.id("elementID"));
Example:
java
WebElement usernameField = driver.findElement(By.id("username"));
usernameField.sendKeys("testuser");
Best Practice:
- Always use By.id() if the element has a unique ID.
- IDs are often static and do not change across sessions, which makes them ideal for automation.
2. Locating Elements by Name
The Name attribute is often used in form inputs, making it a handy locator for fields like usernames, passwords, and search boxes.
Syntax:
java
WebElement element = driver.findElement(By.name("elementName"));
Example:
java
WebElement searchField = driver.findElement(By.name("q"));
searchField.sendKeys("Selenium Java");
Best Practice:
- Ensure the name attribute is unique or scoped within a specific section of the DOM.
- Use it when id is unavailable or dynamic.
3. Locating Elements by Class Name
The Class Name locator allows you to target elements using the class attribute. It's useful for locating elements styled similarly, such as buttons or labels.
Syntax:
java
WebElement element = driver.findElement(By.className("className"));
Example:
java
WebElement loginButton = driver.findElement(By.className("btn-login"));
loginButton.click();
Important Note:
If an element has multiple classes, By.className() expects only one class name.
html
<div class="btn primary">Click</div>
You should use:
java
driver.findElement(By.className("btn")); // Not "btn primary"
Best Practice:
- Avoid using class names that are common across multiple elements unless you use it with context (e.g., within a specific parent)
Handling NoSuchElementException
Sometimes, even if your locator syntax is correct, Selenium may throw a NoSuchElementException. This could be due to:
- The element not being loaded yet.
- A typo in the locator.
- The element being inside an iframe or shadow DOM.
Solution:
- Use WebDriverWait to wait for the element:
java
WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(10));
WebElement element = wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("username")));
Final Thoughts
Mastering the use of ID, Name, and Class Name locators is essential for any Selenium Java automation tester. These locators are easy to use, efficient, and work well for most static elements. However, always inspect the HTML structure and choose the locator strategy that ensures reliability and maintainability.
In the long run, combining these basic locators with advanced strategies like XPath or CSS selectors will give you the flexibility to handle even the most complex web elements with ease.
Learn Selenium with Java Training
Read More: Writing Your First Selenium Java Test Script
Visit Our IHUB Talent Institute Hyderabad
Get Direction
Comments
Post a Comment