skip to content

Standard Locator Types

Eight By factories, but only five locator strategies exist on the wire, so the extras are rewritten into CSS before the request is sent. Interviewers ask which is which and why.

on this pageshow

explore

questions

5

In Selenium 4, which of the eight By factories map onto a WebDriver wire locator strategy of their own?

level: seniorimportance: must knowfreq 72%

basics

~10 s

Five of them: By.cssSelector, By.linkText, By.partialLinkText, By.tagName and By.xpath. By.id, By.name and By.className have no W3C strategy, so the client turns each into an equivalent CSS selector instead.

open as a page

In Selenium 4, why does a By.id lookup reach the remote end as a css selector find?

level: seniorimportance: must knowfreq 46%

basics

~20 s

Because id is not a WebDriver locator strategy. The Java client first tries sending using id, the remote end refuses it with invalid argument, and the client then repeats the lookup with the CSS fallback the By carries.

open as a page

In Selenium 4, why does By.className("recall-row overdue") throw before any command is sent?

level: middleimportance: should knowfreq 58%

basics

~10 s

Compound class names are rejected. The By.className constructor checks the value for whitespace and throws InvalidSelectorException immediately, because its CSS fallback is a single class selector that could never match two tokens.

open as a page

In Selenium 4, why can By.id("recall.due") find an element that By.cssSelector("#recall.due") cannot?

level: seniorimportance: should knowfreq 31%

basics

~10 s

Escaping. Selenium rewrites By.id into a CSS selector but escapes the value first, so the dot stays a literal character of the id. A hand-written selector reads that same dot as a class instead.

open as a page