Locators: ID, Name, ClassName, TagName
A locator is how you tell Selenium which element on the page you mean. Every locator strategy
is exposed through the By class (org.openqa.selenium.By), and you pass the
result of a By.xxx(...) call into driver.findElement(...) or
driver.findElements(...) (plural, returns a list). This lesson covers the four simplest
and most commonly usable strategies: id, name, className, and
tagName.
By.id() — Locate by the id Attribute
The id attribute is meant by HTML convention to be unique on a page, which makes
By.id() the fastest and most reliable locator whenever one is available. RecipeBox's
login page has exactly this:
// <input type="email" id="email">
WebElement emailField = driver.findElement(By.id("email"));
emailField.sendKeys("tester@example.com");
// <input type="password" id="password">
driver.findElement(By.id("password")).sendKeys("Test@1234");
// <button id="loginBtn">Log In</button>
driver.findElement(By.id("loginBtn")).click();
id internally in
constant time (it's how document.getElementById works too), and because ids are supposed
to be unique, there's no ambiguity about which element you'll get back. Whenever a developer has given
an element a stable id, prefer it over every other strategy in this lesson.By.name() — Locate by the name Attribute
Not every element has an id, but form fields frequently carry a name
attribute (it's what gets submitted with traditional HTML forms). Suppose RecipeBox's search box
didn't have an id but did have name="q":
// <input type="text" name="q">
driver.findElement(By.name("q")).sendKeys("Butter Chicken");
id, name is not required to be unique
on a page — radio button groups, for example, routinely share the same name on
purpose. If multiple elements share a name, findElement returns only the
first match in DOM order, which can silently grab the wrong element.By.className() — Locate by CSS Class
Every recipe card in RecipeBox's recipe list might be rendered with a shared CSS class, e.g.
class="recipe-card". By.className() finds elements carrying that class:
// <div class="recipe-card">Paneer Butter Masala</div>
// <div class="recipe-card">Butter Chicken</div>
List<WebElement> recipeCards = driver.findElements(By.className("recipe-card"));
System.out.println("Recipes found: " + recipeCards.size());
By.className() —
it does not accept a space-separated list like "recipe-card featured". If an element has
multiple classes and you need to match more than one, use a CSS selector instead (next lesson).By.tagName() — Locate by HTML Tag
The bluntest locator: match every element with a given tag name. Useful for grabbing all links, all
table rows, or (as below) every <option> inside RecipeBox's category dropdown:
// <select id="categoryDropdown">
// <option>Breakfast</option>
// <option>Main Course</option>
// <option>Dessert</option>
// </select>
WebElement dropdown = driver.findElement(By.id("categoryDropdown"));
List<WebElement> options = dropdown.findElements(By.tagName("option"));
System.out.println("Available categories: " + options.size());
dropdown.findElements(...) was called
on the WebElement, not on driver. This searches only inside that dropdown
rather than the whole page — a useful habit once pages get larger.Which One Should You Use?
| Locator | Speed / stability | Use when |
|---|---|---|
By.id() | Fastest, most stable | An id is present — almost always your first choice |
By.name() | Fast, reasonably stable | No id, but the form field has a stable name attribute |
By.className() | Fast, but classes often change with styling updates | Finding a group of similarly-styled elements (e.g., all recipe cards) |
By.tagName() | Fast, but rarely unique enough alone | Combined with another locator, or counting all elements of one type |
id on every
element you need. Head to CSS Selectors to learn a far more
flexible locator strategy.