We use cookies to analyze site traffic and show personalized ads. You can accept all cookies or decline personalized advertising.
Learn more in our Privacy Policy.
Learn the 8 traditional locator strategies and when to use each.
By class.Every Selenium test you write depends on finding the right element on the page. If your locators are weak or unstable:
Understanding the different locator strategies—and when to use each—helps you:
Before starting this lab, you should:
By class to define locators (e.g. By.id("username")).id, name, class).<input>, <button>, <a>, <div>.Selenium WebDriver provides 8 basic ways to locate elements on a web page via the By class:
By.id()By.name()By.className()By.tagName()By.linkText()By.partialLinkText()By.cssSelector()By.xpath()All of these are ultimately used with findElement / findElements, for example:
driver.findElement(By.id("username"))driver.findElements(By.tagName("input"))Each strategy has its strengths, weaknesses, and ideal use cases.
In real projects, you don't choose locators randomly. A good practical priority order is:
username, email, password).id or name works, you usually don't need a long CSS or XPath.By.id()What it does
Selects the element with a specific id attribute.
Why it's good
Example
id="user-123" → By.id("user-123")
id, this should usually be your first choice.By.name()What it does
Selects the element with a specific name attribute.
Typical use
Form fields: name="username", name="email", name="password".
Notes
Can be unique, but sometimes multiple elements share the same name.
Example
name="username" → By.name("username")
By.className()What it does
Selects elements with a specific CSS class.
Typical use
Styling groups of elements, e.g. class="form-control", class="btn-primary".
Risk
Classes are often shared between many elements, so they might not be unique.
Example
class="form-control" → By.className("form-control")
className alone if that class is used on many elements.By.tagName()What it does
Selects elements by HTML tag (e.g. input, button, a, tr).
Typical use
When you want a list of elements, like all rows in a table or all inputs in a form.
Risk
Almost never unique by itself.
Example
All <input> elements → By.tagName("input")
By.linkText()What it does
Selects a link (<a> tag) by its exact visible text.
Limitation
Only works for <a> elements, not for <button> or other tags.
Example
Link with text Login → By.linkText("Login")
By.partialLinkText()What it does
Selects a link (<a>) whose visible text contains the given substring.
Typical use
When the link text is long but has a unique part (e.g. "Log" in "Login here to your account").
Risk
Might match multiple links if they share similar text.
Example
Text includes "Log" → By.partialLinkText("Log")
By.cssSelector()What it does
Uses CSS selector syntax to target elements.
Why it's powerful
Examples
#user-123.btn-primaryinput[type="email"]By.xpath()What it does
Uses XPath expressions to target elements.
Why it's powerful
Examples
//input[@id='user-123']//button[text()='Login']id when unique and stable.name for form inputs when id is missing.linkText / partialLinkText only for <a> links.tagName or className unless you are intentionally getting a collection.A very common mistake is relying on locators that depend on position only, such as:
These locators break easily when:
Instead, prefer using meaningful attributes (id, name, data-* attributes) or text where appropriate.
id, name, or even custom data-testid attributes specifically to support test automation.id, name, class, tag name (e.g. input, button)Relate each attribute to one of the locator strategies.
Remember the recommended order:
idnamecssSelectorxpathThink about why each is in that position:
id, name, or short CSS selector could solve it.Pick 3–5 elements on a form:
For each element:
id (if any).name.class values.Decide which locator you'd use and why.
input-1047 that might change on each reload. They look like ids but are not stable.On a page with multiple inputs or links:
<input> elements exist (conceptually).By.tagName("input") would return all of them.Understand that tagName is best when you expect multiple matches, not a single unique element.
tagName combined with filtering (in your code logic) can be useful when you need to iterate through a list of similar elements, like table rows.Look at links in a navigation bar or footer:
Ask:
linkText?partialLinkText be safe here (or would it match multiple links)?linkText (exact match) when the link text is short and stable. Use partialLinkText only when necessary and when the partial text is truly unique.Pick one target element and imagine:
Compare:
Think of examples of fragile locators, such as:
(//button)[3]Rewrite each fragile locator in your mind using a more stable strategy.
/div/div/div[3]) is a red flag for future flakiness.This lab is conceptual only, so we are not writing full Selenium code here.
For now, just remember the general pattern you will use in later labs:
driver.findElement(By.<strategy>("value"))You will apply each locator type in real Selenium code in upcoming hands-on labs.
id, name, or data-testid) to important elements.By.tagName or By.className expecting a single element when they match many./div[2]/div[4]/... that break on minor layout changes.If you master these locator strategies and choose them wisely, your Selenium tests will be much more stable and easier to maintain as your application evolves.