skip to content

Addressing Methods

The built-in ways to name an element and what each one really does: the eight By factories, first-match versus list results, and re-searching inside an element you already hold.

on this pageshow

explore

questions

12

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, why does a test asserting that a violation banner has disappeared pass even when it is still displayed?

level: seniorimportance: must knowfreq 68%

basics

~20 s

Absence and a broken locator look identical to Selenium: both produce no match. Check absence with findElements and isEmpty, and prove the same locator matches while the banner is present, or the assertion passes for the wrong reason.

open as a page

In Selenium, what changes when you call findElement on a WebElement instead of on the WebDriver?

level: juniorimportance: should knowfreq 58%

basics

~10 s

Selenium's WebDriver and WebElement both implement SearchContext, so an element can start a search of its own. A find called on an element sends a find-from-element command and searches only that element's descendants.

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, what does findElement do when a locator matches twelve rows, and why is the resulting bug silent?

level: middleimportance: should knowfreq 58%

basics

~20 s

It returns element zero of the match list in document order and never signals that others matched, so the test acts on the first row and still passes. Only counting the plural call reveals the ambiguity.

open as a page

In Selenium, why can an XPath starting with // still match outside the WebElement you searched from?

level: middleimportance: should knowfreq 51%

basics

~20 s

A leading slash in XPath means the document root, not the element you are searching from. The element is used only to find the document, so the expression scans the whole page. Start it with a dot instead.

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

In Selenium, findElement silently returns the first of many matches; should a suite enforce single-match semantics, and what does that cost?

level: principalimportance: should knowfreq 34%

basics

~10 s

Selenium has no strict-match mode, so enforcing one means a helper that calls findElements and fails unless exactly one element matched. That turns silent wrong-target bugs into loud failures, but rejects legitimately repeated markup.

open as a page

In Selenium, can driver.findElement return null when nothing matches, or does it throw?

level: juniorimportance: nice to knowfreq 44%

basics

~10 s

It never returns null. Selenium's findElement throws NoSuchElementException when nothing matches, so a null check after it is unreachable code, while the plural findElements returns an empty list instead of throwing.

open as a page

In Selenium, why does re-finding a parent WebElement not refresh the child elements found through it?

level: seniorimportance: nice to knowfreq 36%

basics

~20 s

A nested find resolves once and returns a handle on one node, with no link back to the parent. Re-finding the parent only re-runs the parent's locator, so the child references still point at the nodes a re-render discarded.

open as a page