Selenium · Browser automation foundations

Which locator strategies does Selenium support and how do you choose?

EasylocatorsbyselectorsUpdated Oct 2026
Quiz this track

Short answer

Selenium supports id, name, className, tagName, linkText, partialLinkText, CSS selectors and XPath. Prefer stable, unique attributes such as data-testid, then id, then CSS; use XPath when you need text or structural relationships.

A locator is a query against the current DOM. Its quality determines how often the test breaks. Stable attributes owned by the team, such as data-testid, are better than generated class names, layout position or long chains of divs.

java
driver.findElement(By.id("email"));
driver.findElement(By.name("password"));
driver.findElement(By.cssSelector("[data-testid='sign-in']"));
driver.findElement(By.linkText("Forgot password"));
driver.findElement(By.partialLinkText("Forgot"));
driver.findElement(By.xpath("//button[normalize-space()='Save']"));
driver.findElements(By.className("row"));
findElement returns the first match; findElements returns all matches.
  • id is fast and unique when the application guarantees it
  • data-testid is explicit and survives styling and copy changes
  • CSS is concise and well supported for attributes and structure
  • XPath handles text matching and axes that CSS cannot express
  • linkText only works for anchor elements and exact visible text
  • className and tagName are usually too broad on their own

Prefer a locator that describes intent, not implementation. A selector like div:nth-child(3) > span breaks when layout changes even though behaviour is unchanged. If the only option is a brittle selector, that is a signal to ask the application team for a test id.

Never locate by generated CSS class names from styled-components or CSS modules. They change on every build and turn the suite into a maintenance tax.

Follow-ups you should expect

Interviewers use these to check whether the first answer was understanding or memorisation.

  1. 1When would you choose XPath over CSS?
  2. 2Why are generated class names a bad locator?
  3. 3What is the difference between findElement and findElements?
  4. 4How do you get the application team to add stable test ids?

Say it in the interview

I prefer a stable owned attribute such as data-testid, then id, then CSS. XPath is for cases where I need text matching or structural relationships, and linkText is only for anchors. I avoid generated class names and positional selectors because they break on unrelated UI changes. If no stable locator exists, I treat that as a product and engineering conversation rather than adding another brittle selector.

Use this as a model, not a script. Saying it in your own words is what makes it survive follow-up pressure.

Related questions

Reading is step one

Now say it out loud.

Understanding an answer and delivering it under pressure are different skills. Rehearse this topic in a live Selenium mock interview and get scored feedback on depth, clarity and confidence.