Skip to main content
SeleniumDecoded

NoSuchShadowRootException

NoSuchShadowRootException: Causes and Fixes

Fix Selenium NoSuchShadowRootException (NoSuchShadowRootError in JS): wrong shadow host, closed shadow roots, late web components and nested shadow DOM.

Occasional W3C: no such shadow root Verified against Selenium 4.52.0
Exception name by language
Java org.openqa.selenium.NoSuchShadowRootException
Python selenium.common.exceptions.NoSuchShadowRootException
JavaScript NoSuchShadowRootError
C# OpenQA.Selenium.NoSuchShadowRootException

What the error means

You called getShadowRoot() (Python shadow_root) on an element, and that element has no shadow root the driver can access. Either it is not a shadow host at all, its shadow root is closed, or the component has not attached its shadow root yet.

Selenium 4 added first-class shadow DOM support: getShadowRoot() returns a search context, and you find elements inside it with CSS selectors. This error is the first thing you hit when the element you called it on is not the right one.

What it looks like

// Java
org.openqa.selenium.NoSuchShadowRootException: no such shadow root
(Session info: chrome=131.0.6778.86)
at org.openqa.selenium.remote.RemoteWebElement.getShadowRoot(RemoteWebElement.java:...)
# Python
selenium.common.exceptions.NoSuchShadowRootException: Message: no such shadow root
(Session info: chrome=131.0.6778.86)
// JavaScript (selenium-webdriver)
NoSuchShadowRootError: no such shadow root
(Session info: chrome=131.0.6778.86)
// C#
OpenQA.Selenium.NoSuchShadowRootException : no such shadow root

Firefox may add the element: The element does not have a shadow root.

Common causes

  1. You called it on the wrong element. The shadow host is the custom element (for example sl-input or my-date-picker), not its parent wrapper and not an element inside the shadow tree.
  2. The shadow root is closed. Components created with attachShadow({ mode: 'closed' }) do not expose their root to scripts or to WebDriver.
  3. The component has not attached its shadow root yet. The custom element is in the DOM but not yet upgraded, because its JavaScript definition loads lazily.
  4. Nested shadow DOM. The element you want is inside a shadow root inside another shadow root, and you skipped a level.
  5. The host was re-rendered. You hold a reference to an old host element; that usually surfaces as StaleElementReferenceException, but a replaced host can also lack a root briefly.

How to fix it

You called it on the wrong element

In DevTools, shadow hosts show a #shadow-root (open) node directly beneath them. Locate exactly that element. Then search inside the root with CSS selectors.

Get the shadow root from the actual host
Selenium 4 Stable
WebElement host = driver.findElement(By.cssSelector("sl-input[name='email']"));
SearchContext root = host.getShadowRoot();
root.findElement(By.cssSelector("input")).sendKeys("qa@example.com");
host = driver.find_element(By.CSS_SELECTOR, "sl-input[name='email']")
root = host.shadow_root
root.find_element(By.CSS_SELECTOR, "input").send_keys("qa@example.com")
const host = await driver.findElement(By.css("sl-input[name='email']"));
const root = await host.getShadowRoot();
await (await root.findElement(By.css('input'))).sendKeys('qa@example.com');
IWebElement host = driver.FindElement(By.CssSelector("sl-input[name='email']"));
ISearchContext root = host.GetShadowRoot();
root.FindElement(By.CssSelector("input")).SendKeys("qa@example.com");

Use CSS inside shadow roots. XPath is not supported there in Chromium-based browsers and fails with InvalidArgumentException or InvalidSelectorException.

The shadow root is closed

There is no supported WebDriver way into a closed shadow root, by design. Your options, best first:

  • Interact through the host’s public surface. Many components forward focus and keyboard input, so clicking the host and typing works. Assert through the host’s attributes or properties (getDomProperty("value")).
  • Ask for a test build with mode: 'open', or a test hook attribute that the component reflects to the light DOM.
  • Use the component’s public JavaScript API via executeScript (for example arguments[0].value = 'x' if the component exposes a value property and fires its own events).

Avoid monkey-patching attachShadow before page load to force open mode. It works, but it changes the application under test.

The component has not attached its shadow root yet

Wait until the host has a shadow root, then use it. A custom wait that retries getShadowRoot() keeps it simple.

Wait for the shadow root to be attached
Selenium 4 Stable
WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(10));
wait.ignoring(NoSuchShadowRootException.class);
SearchContext root = wait.until(
d -> d.findElement(By.cssSelector("my-date-picker")).getShadowRoot());
from selenium.common.exceptions import NoSuchShadowRootException
wait = WebDriverWait(driver, 10, ignored_exceptions=[NoSuchShadowRootException])
root = wait.until(
lambda d: d.find_element(By.CSS_SELECTOR, "my-date-picker").shadow_root)
const { error } = require('selenium-webdriver');
const root = await driver.wait(async () => {
try {
return await driver.findElement(By.css('my-date-picker')).getShadowRoot();
} catch (e) {
if (e instanceof error.NoSuchShadowRootError || e instanceof error.NoSuchElementError) return false;
throw e;
}
}, 10000);
var wait = new WebDriverWait(driver, TimeSpan.FromSeconds(10));
wait.IgnoreExceptionTypes(typeof(NoSuchShadowRootException));
ISearchContext root = wait.Until(
d => d.FindElement(By.CssSelector("my-date-picker")).GetShadowRoot());

Alternatively wait for the component definition itself: executeScript("return !!customElements.get('my-date-picker');") returns true once the element is defined.

Nested shadow DOM

Step through each level: find the outer host, get its root, find the inner host inside that root with CSS, get its root, and so on. There is no single selector that pierces shadow boundaries in WebDriver.

Walk nested shadow roots one level at a time
Selenium 4 Stable
SearchContext appRoot = driver.findElement(By.cssSelector("my-app")).getShadowRoot();
SearchContext formRoot = appRoot.findElement(By.cssSelector("checkout-form")).getShadowRoot();
formRoot.findElement(By.cssSelector("button[type='submit']")).click();
app_root = driver.find_element(By.CSS_SELECTOR, "my-app").shadow_root
form_root = app_root.find_element(By.CSS_SELECTOR, "checkout-form").shadow_root
form_root.find_element(By.CSS_SELECTOR, "button[type='submit']").click()
const appRoot = await driver.findElement(By.css('my-app')).getShadowRoot();
const formRoot = await (await appRoot.findElement(By.css('checkout-form'))).getShadowRoot();
await (await formRoot.findElement(By.css("button[type='submit']"))).click();
ISearchContext appRoot = driver.FindElement(By.CssSelector("my-app")).GetShadowRoot();
ISearchContext formRoot = appRoot.FindElement(By.CssSelector("checkout-form")).GetShadowRoot();
formRoot.FindElement(By.CssSelector("button[type='submit']")).Click();

Wrap the chain in a helper that takes a list of host selectors; the nested shadow DOM recipe has one.

The host was re-rendered

Re-find the host immediately before calling getShadowRoot(), and do not cache shadow root objects across actions that re-render the component. A shadow root reference goes stale (detached shadow root) the same way an element does.

How to prevent it

  • Document the host element for each web component in your page objects, and keep the root-walking logic in one helper.
  • Use CSS selectors exclusively inside shadow roots.
  • Wait for components to upgrade before using them, especially on lazily loaded pages.
  • Agree with developers that test-relevant components use open shadow roots, or expose test hooks.
  • Read the shadow DOM lesson for the full picture, including relative locators anchored to shadow roots (Java and .NET, Selenium 4.50+).

Go deeper