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.
How findElement behaves when multiple elements match, and what "search the whole DOM" really means.
findElement behaves when multiple elements match the same locator.driver.findElement(...).Many beginners assume that Selenium "knows" which element they want when multiple elements match a locator. In reality, Selenium simply returns the first match in DOM order, which may not be the element you expected.
Understanding this behavior helps you:
findElement vs findElements.Before starting this lab, you should:
findElement and findElements.driver.findElement(locator), Selenium returns only the first matching element in DOM order. If there are 10 matching elements, you still get just one – the first one it finds. "First" means first in the HTML structure, not the one you see first on the screen.driver.findElement(...), the search root is the entire page (current browsing context). Selenium evaluates the whole DOM until it finds the first match. (WebDriver implements SearchContext, making the driver the search starting point.)findElement returns the first match as a single WebElement.findElement throws NoSuchElementException; findElements returns an empty list.Imagine a simple page with two lists and one button:
class="tomatoes")class="tomatoes")Text: "Find Me & Click Me (First Match)"
Class: class="lesson2-practice-btn"
class="tomatoes":#vegetables (appears first in DOM)#fruits (appears second in DOM)By.className("tomatoes") with findElement, Selenium will return the vegetables tomato – not because it's more important, but simply because it appears first in the HTML.12345678910111213By tomatoesLocator = By.className("tomatoes");
// First match in the whole DOM (driver = page root)
WebElement firstTomatoItem = driver.findElement(tomatoesLocator);
String firstTomatoText = firstTomatoItem.getText();
By practiceButtonLocator = By.className("lesson2-practice-btn");
// First matching button with this class
WebElement practiceButton = driver.findElement(practiceButtonLocator);
practiceButton.click();
driver.quit();By tomatoesLocator = By.className("tomatoes")Defines a non-unique locator that matches both tomato list items (vegetable and fruit).
driver.findElement(tomatoesLocator)Because we call findElement on driver, Selenium searches the entire page (whole DOM) and returns the first matching element in DOM order.
WebElement firstTomatoItem = driver.findElement(tomatoesLocator)Stores the first match (the tomato inside the Vegetables list) as a WebElement.
String firstTomatoText = firstTomatoItem.getText()Reads the visible text of that item. For this page, it will be "Tomato is a Vegetable" – not the fruit one.
By practiceButtonLocator = By.className("lesson2-practice-btn")Locator for the practice button, using its class attribute.
WebElement practiceButton = driver.findElement(practiceButtonLocator)Again, findElement returns the first button on the page that has class "lesson2-practice-btn".
practiceButton.click()Clicks that first matching button.
driver.quit()Closes the browser and ends the WebDriver session, cleaning up resources.
findElements(locator).get(1) orBy.cssSelector("#fruits .tomatoes")$$("your-selector") for CSS. Make sure you understand how many elements it matches and which ones.By.className("btn")are easy to write, but they often give you the wrong "first match".Selenium doesn't read your mind. If your locator matches 5 buttons, it will always return the first one in DOM order, not the one you're thinking about.
Just because a button appears at the bottom of the screen doesn't mean it's last in the HTML. CSS can move elements anywhere visually while keeping their DOM position unchanged.
By.className("btn") or By.tagName("button") will likely match dozens of elements. Unless you explicitly want the first match, make your locator more specific.
Always verify your locator in the browser console ($$("selector") for CSS) before putting it in your code. This shows you exactly how many elements match and in what order.
When you use driver.findElement(...), the search starts from the page root and covers the entire DOM. If you want to search within a specific section, find that section first, then call .findElement(...) on it (covered in later lessons).