Testing / Selenium Interview questions
How does Selenium handle elements inside Shadow DOM?
Unlike Playwright's locators, Selenium's classic By strategies do not automatically pierce shadow roots - a plain CSS selector or XPath simply cannot see past a shadow boundary into its contents.
WebElement host = driver.findElement(By.cssSelector("custom-element")); SearchContext shadowRoot = host.getShadowRoot(); WebElement inner = shadowRoot.findElement(By.cssSelector("button"));
Selenium 4 added the getShadowRoot() method on WebElement, returning a SearchContext that findElement()/findElements() can then be called on to search specifically within that shadow tree - but it only works on open shadow roots, and each nested shadow boundary needs its own explicit getShadowRoot() call, unlike a single chained selector that pierces automatically. Closed shadow roots remain inaccessible, the same fundamental browser-level restriction every automation tool runs into.
Before Selenium 4 added native support, teams commonly worked around this gap entirely with JavascriptExecutor, calling element.shadowRoot directly through injected JavaScript - a pattern that's now largely unnecessary but still shows up in older Selenium codebases.
More Related questions...