skip to content

Element Location

How a Selenium client names the element it wants: the By strategies, what a find call hands back, the CSS and XPath expression languages, and reaching into a shadow root.

on this pageshow

explore

questions

page 1 of 2

In Selenium, why does By.cssSelector fail on an element inside a web component's shadow tree?

level: juniorimportance: must knowfreq 68%

basics

~20 s

The find command searches one node tree from a single start node, and a shadow tree is a separate tree, so a descendant selector never reaches inside. Find the host, take its shadow root, search again from there.

open as a page

In Selenium, what does each driver.findElement call cost when a test loops over many rows?

level: juniorimportance: must knowfreq 66%

basics

~20 s

Each findElement call is one HTTP command to the browser driver, sent as POST /session/{id}/element. A loop over 300 rows sends 300 separate commands, and those round trips add up no matter how simple the selector is.

open as a page

In Selenium, why can a CSS locator never match an element by its visible text?

level: middleimportance: must knowfreq 72%

basics

~20 s

Because a CSS selector matches on tags, ids, classes, attributes, structural position and state, never on the characters inside an element. Selenium sends the string straight to the browser's querySelectorAll, so a locator gets exactly that power and no more.

open as a page

In Selenium 4, what geometric rule does RelativeLocator's above() apply, and why do overlapping elements match no direction?

level: middleimportance: must knowfreq 62%

basics

~20 s

Selenium compares bounding rectangles edge to edge: a candidate is above an anchor only when the candidate's bottom edge sits at or higher than the anchor's top edge. Any overlap fails all four directional tests.

open as a page

In Selenium 4, what does RelativeLocator's near() match by default, and how are its results ordered?

level: middleimportance: must knowfreq 58%

basics

~10 s

Selenium's near() keeps any element whose rectangle overlaps the anchor's rectangle grown by 50 pixels on every side, excluding the anchor itself. Results come back sorted by centre-to-centre distance, closest first.

open as a page

In Selenium XPath, why does //tr[1] match several rows while (//tr)[1] matches only one?

level: middleimportance: must knowfreq 66%

basics

~10 s

A predicate attaches to the step in front of it and is evaluated once per context node, so //tr[1] keeps the first row under every parent. Parentheses index the finished node-set instead.

open as a page

In Selenium 4, what does the browser actually run for a By.cssSelector find versus a By.xpath find?

level: middleimportance: must knowfreq 74%

basics

~20 s

A CSS find makes the browser call querySelectorAll on its native selector engine. An XPath find makes it call evaluate in the separate DOM-XPath evaluator. Both run inside one WebDriver command, so the gap is usually tiny.

open as a page

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, can a CSS locator select an element by what it contains, and when must XPath take over?

level: seniorimportance: must knowfreq 58%

basics

~20 s

Yes, with the :has() relational pseudo-class, as long as the browser under test implements it. It still cannot test rendered text, so a lookup that depends on what an element reads must fall back to an XPath expression.

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, why does a stored shadow-root reference start failing after its component re-renders?

level: seniorimportance: must knowfreq 44%

basics

~20 s

A shadow root is a handle the browser resolves on every command, and it stays valid only while its host element does. Once the host is replaced the root is detached and every lookup through it fails.

open as a page

A Selenium test reading 300 review-queue rows is slow, and swapping every XPath for CSS barely helped. Why?

level: seniorimportance: must knowfreq 57%

basics

~20 s

Because the time is going into the number of commands, not the matching engine. Three hundred finds plus three hundred text reads is six hundred round trips, and a faster in-page match saves only microseconds on each one.

open as a page

In Selenium 4, where does a RelativeLocator lookup actually execute, and what does that placement cost?

level: principalimportance: must knowfreq 38%

basics

~20 s

It runs inside the page. Selenium injects a bundled JavaScript atom with one execute-script call, which resolves the locators and reads every rectangle. So matching semantics are pinned to your client version, not to the browser driver.

open as a page

In Selenium, what happens when By.cssSelector is given a string the browser cannot parse?

level: juniorimportance: should knowfreq 55%

basics

~20 s

Nothing checks the string locally. It travels to the browser, which runs querySelectorAll with it; the parse failure comes back as the invalid selector error and the Java client raises InvalidSelectorException, which is not the same as NoSuchElementException.

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, what does WebElement.getShadowRoot() return, and what can you do with it?

level: juniorimportance: should knowfreq 52%

basics

~20 s

It returns a SearchContext for the element's shadow root, so the only things you can call on it are findElement and findElements. It is not a WebElement, so there is nothing on it to click or read text from.

open as a page

In Selenium, how does an absolute XPath differ from a relative XPath that begins with a double slash?

level: juniorimportance: should knowfreq 78%

basics

~10 s

An absolute XPath spells out every step from the document root, such as /html/body/table/tbody/tr[2]/td[1]. A path starting with a double slash searches the whole document and matches at any depth.

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, why can no CSS locator find a ::before pseudo-element, and how do you read its content?

level: middleimportance: should knowfreq 38%

basics

~20 s

A pseudo-element is generated by the style engine and never becomes a DOM node, so the browser's querySelectorAll cannot return one and Selenium has nothing to wrap in a WebElement. Read it with getComputedStyle inside an injected script instead.

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, how do you reach an element nested inside two levels of shadow root?

level: middleimportance: should knowfreq 40%

basics

~10 s

You cross one boundary at a time: find the outer host, take its root, find the inner host inside that root, take its root, then find your element. That is five commands.

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

A Selenium relative locator chaining below() and toRightOf() returns nothing after a layout change — what do you check?

level: seniorimportance: should knowfreq 41%

basics

~10 s

Bisect the chain: run the root locator alone, then with each filter added, to find which filter empties the set. Then compare the anchor and candidate rectangles, since the directional filters reject any overlap.

open as a page

In Selenium XPath, why does ancestor::tr[1] select the nearest row rather than the outermost one?

level: seniorimportance: should knowfreq 42%

basics

~10 s

The ancestor axis is a reverse axis, so XPath numbers its positions outward from the context node: index 1 is the closest matching ancestor, not the outermost. Forward axes number in document order instead.

open as a page

In Selenium, what does an absolute XPath like /html/body/div[2]/table/tr[7]/td[3] cost on every find call?

level: seniorimportance: should knowfreq 45%

basics

~10 s

It is re-evaluated from the document root on every call, because WebDriver hands the expression to the browser afresh each time. The walk is cheap per step; a leading double slash is what costs.

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

showing 1–30 of 32