Selenium Shadow DOM

Shadow DOM hides a small section of a web page behind a private wall. Imagine an apartment building. The main page is the street view, and each apartment has a locked door. Normal locators walk along the street and never enter an apartment. Components such as date pickers, video players, and custom buttons often live in these private rooms, and ordinary Selenium searches return no match for them.

Why Developers Use It

Shadow DOM protects a component. Style rules from the outer page cannot leak inside, and the component's own styles cannot leak out. Developers get reusable widgets that behave the same on every site. Testers face a hidden structure that needs a special approach.

Structure Diagram

document
 +-- div#app
      +-- my-calendar            (shadow host)
           +-- #shadow-root      (private wall)
                +-- div.header
                +-- button.next   (hidden from normal search)
TermMeaning
Shadow hostRegular element that carries the hidden section
Shadow rootEntry point of the hidden section
Open modeOutside code may enter the shadow root
Closed modeOutside code stays locked out

Spotting a Shadow Root

Open browser developer tools and inspect the element. A line reading #shadow-root (open) confirms the wall. A locator that works in the developer console search box but fails in Selenium often signals a shadow root.

The Selenium 4 Method

Selenium 4 added getShadowRoot(). Locate the host element, request its shadow root, and search inside it.

WebElement host = driver.findElement(By.tagName("my-calendar"));
SearchContext shadow = host.getShadowRoot();
WebElement nextButton = shadow.findElement(By.cssSelector("button.next"));
nextButton.click();
host element --getShadowRoot()--> shadow root --findElement()--> inner element

Important Locator Rule

Searches inside a shadow root accept CSS selectors, id, name, and tag name. XPath does not work there. XPath cannot cross the private wall. Plan inner locators around CSS.

Nested Shadow Roots

Some components hold another component, which holds another. Walk through each level in order.

page -> host A -> shadow A -> host B -> shadow B -> target
SearchContext levelOne = driver.findElement(By.cssSelector("host-a")).getShadowRoot();
SearchContext levelTwo = levelOne.findElement(By.cssSelector("host-b")).getShadowRoot();
levelTwo.findElement(By.cssSelector("input.search")).sendKeys("laptop");

JavaScript Fallback

Older Selenium versions lack getShadowRoot(). A JavaScript snippet reaches the same element.

JavascriptExecutor js = (JavascriptExecutor) driver;
WebElement inner = (WebElement) js.executeScript(
    "return document.querySelector('my-calendar').shadowRoot.querySelector('button.next');");
inner.click();

Closed shadow roots refuse both methods. Ask the development team to open the root in the test build or expose a test attribute.

Combine with Waits

Components often render after the page loads. Wait for the host element first, request the root, and apply a wait inside the root when needed.

WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(10));
WebElement host = wait.until(ExpectedConditions
        .presenceOfElementLocated(By.tagName("my-calendar")));
SearchContext shadow = host.getShadowRoot();

Real Examples of Shadow DOM

  • Browser settings pages such as chrome://downloads use nested shadow roots.
  • Web component libraries ship buttons, menus, and dialogs with shadow roots.
  • Embedded video players hide their controls inside the wall.

Key Points

  • Shadow DOM hides component internals from normal locators.
  • Find the host, call getShadowRoot, and search with CSS.
  • Walk each level of nested roots in order.
  • Closed roots need help from the development team.

A Layman Walkthrough

Suppose a page shows a custom date picker. A human sees a calendar and clicks the next-month arrow. The tester inspects the arrow and finds it inside a shadow root. The plan has three moves: locate the calendar tag, open its private room, and click the arrow with a CSS selector. Each move matches one line of code, and the order never changes.

Tips for Stable Shadow Tests

Create small helper methods that accept a host locator and an inner locator, and return the inner element. A helper keeps shadow logic in one place. A change in component structure needs one fix instead of fifty. Ask developers to add data-testid attributes to inner elements. Test attributes survive design changes. Record the component library version in your test notes, since upgrades sometimes rename inner classes.

public WebElement inShadow(String hostCss, String innerCss) {
    SearchContext root = driver.findElement(By.cssSelector(hostCss)).getShadowRoot();
    return root.findElement(By.cssSelector(innerCss));
}

inShadow("my-calendar", "button.next").click();