To click a button, you first need to find it. To type in a text field, you first need to find it. Everything in Selenium starts with finding elements. You tell Selenium WHERE the element is using a locator, and it gives you back a WebElement object you can interact with.
Selenium gives you 8 ways to find an element. Here's every single one with real examples:
// 1. By.id() — BEST choice. IDs are unique by HTML spec.
WebElement userId = driver.findElement(By.id("userId"));
// 2. By.name() — Good for form fields.
WebElement password = driver.findElement(By.name("password"));
// 3. By.className() — Works with a SINGLE class name only.
WebElement card = driver.findElement(By.className("product-card"));
// 4. By.tagName() — Finds by HTML tag. Rarely unique.
WebElement heading = driver.findElement(By.tagName("h1"));
// 5. By.linkText() — For <a> tags. Must match EXACT text.
WebElement link = driver.findElement(By.linkText("Forgot Password?"));
// 6. By.partialLinkText() — For <a> tags. Partial match.
WebElement reg = driver.findElement(By.partialLinkText("Register"));
// 7. By.cssSelector() — Your go-to for everything.
WebElement btn = driver.findElement(
By.cssSelector("button[data-testid='loginBtn']"));
// 8. By.xpath() — Most powerful. Use when CSS can't do it.
WebElement h2 = driver.findElement(
By.xpath("//h2[text()='Sign In to Your Account']"));| Priority | Strategy | When to Use | Reliability |
|---|---|---|---|
| 1st | By.id() | Always try this first — IDs are unique | Highest |
| 2nd | data-testid (CSS) | By.cssSelector("[data-testid='loginBtn']") — designed for testing | Highest |
| 3rd | By.name() | Form fields with name attributes | High |
| 4th | By.cssSelector() | Everything else — fast and flexible | High |
| 5th | By.xpath() | Text matching, parent/sibling traversal | Medium |
| Last | className, linkText, tagName | Only when nothing else works | Low |
The priority is: id → data-testid → name → CSS → XPath. Most automation engineers use CSS selectors for 80% of their locators. XPath is powerful but slower and harder to read. Never use auto-generated XPaths from browser tools — they break when the page changes.
// findElement() — returns ONE element. Throws exception if not found.
WebElement single = driver.findElement(By.id("userId"));
// findElements() — returns a LIST. Returns empty list if not found.
List<WebElement> allInputs = driver.findElements(By.tagName("input"));
System.out.println("Found " + allInputs.size() + " input fields");
// Pro tip: check if an element exists WITHOUT throwing an exception
boolean errorVisible = driver.findElements(
By.cssSelector("[data-testid='errorMsg']")).size() > 0;findElement() throws NoSuchElementException immediately if nothing matches. findElements() returns an empty list — no exception. Use findElements().size() > 0 to safely check if an element exists. This is a common interview question.
Q: What is the difference between findElement() and findElements()?
A: findElement() returns a single WebElement — the first match. If no element is found, it throws NoSuchElementException. findElements() returns a List<WebElement> of all matches. If nothing is found, it returns an empty list without throwing any exception. I use findElements().size() > 0 to safely check if an element exists on the page.
Q: What happens if findElement() cannot find the element?
A: It throws NoSuchElementException. The common causes are: (1) the element hasn't loaded yet — timing issue, fix with explicit waits, (2) the locator is wrong — typo or incorrect selector, (3) the element is inside an iframe — need to switch to the iframe first, (4) the element is in a different browser window — need to switch windows.
Q: How many locator strategies does Selenium support?
A: Selenium supports 8 locator strategies: By.id(), By.name(), By.className(), By.tagName(), By.linkText(), By.partialLinkText(), By.cssSelector(), and By.xpath(). The recommended priority is: id → data-testid → name → CSS → XPath. id is the fastest and most reliable, XPath is the most powerful but slowest.
Key Point: findElement() finds one element (or throws). findElements() finds all matches (or returns empty list). Priority: id → data-testid → CSS → XPath.