Switch to v2 to see which selectors break.
Add to Chrome

Shadow DOM practice website

Practice locating elements inside shadow roots: an open root, a root inside a root, a closed root that nothing outside can reach, and a design-system button with a slot, a part and a closed root. Each example comes in a well-built form and a fragile one. Open the admin product editor

Coupon fieldAn input and a button inside a shadow root.

Built well

Built to break

Two roots deepA button inside a root that sits inside another root.

Built well

Built to break

Date pickerThe same picker the admin product editor uses.

Built well

Available from

Built to break

Available from

Iframe inside a shadow rootCross the root, then switch into the frame.

Built well

Built to break

Design-system buttonThe label is slotted in from outside; the button lives in the root.

Built well

Checkout

Built to break

Pay now

Tasks: write a locator, get a grade

1Find the Apply coupon button inside the open shadow root.Open root
2Find Add to wishlist, two shadow roots deep.Nested roots
3Find the real button inside the design-system Checkout, not its slotted label.Slot vs button
4The Pay now button uses a closed root. Find the one part of it you can still reach: its slotted label.Closed root
How to locate Shadow DOM

A selector stops at the shadow root

A shadow root is a separate DOM tree attached to a host element. A CSS selector or XPath run against the page stops at the host: document.querySelector('input') will not find an input inside a root, even though you can see it on screen. That is why a locator copied from DevTools often fails on a web component.

Each tool crosses the boundary its own way. Playwright’s CSS, text and role locators pierce open roots on their own, but its XPath does not. Selenium 4 needs an explicit getShadowRoot() (Java) or shadow_root (Python), and the search context it returns accepts CSS selectors only. Cypress needs .shadow() on the host, or includeShadowDom: true in its config.

Nested roots repeat the problem at every level. In Selenium and Cypress you cross once per host, so a component built from components can need three or four hops before you reach the button you want.

Closed roots are a different kind of hard. A root created with mode: 'closed' returns null from element.shadowRoot, and neither WebDriver nor Playwright can reach inside it. The fragile forms on this page use closed roots, so none of the code below works on them. When you meet one in a real app, the fix is a conversation, not a cleverer selector: ask for an open root, or for a test hook on the host.

Design-system components add two more twists. The label you see is often light-DOM content projected through a <slot>: it belongs to the page, not the root, so a plain selector finds the label while the real <button> wrapping it sits inside the root. Clicking either works, because the click bubbles to the host, but asserting on the button’s state needs the button. A part="control" attribute is a styling hook for ::part(); it is a readable attribute inside an open root but cannot be used as a selector from outside. When the root is closed, the slotted label is the only thing left to reach.

Inside an open root, the usual rules still apply. Generated class names change between builds there too, so a locator that crosses the boundary correctly can still break on the next release.

What a stable locator looks like

Find the host by a test id, cross into its root, then find the element by its label, role or test id. In Playwright the crossing happens for you, so a role or label locator reaches the coupon field directly. The date picker names each day with its full date, so you can pick December 12 without counting cells, which matters because v2 starts the week on Sunday and every day moves one column.

// Open roots are pierced automatically (XPath is the exception)
await page.getByLabel('Coupon code').fill('SAVE10');
await page.getByRole('button', { name: 'Apply coupon' }).click();

// Two roots deep: still one locator
await page.getByRole('button', { name: 'Add to wishlist' }).click();

// Each day is named with its full date
await page.getByRole('button', { name: 'December 12, 2026' }).click();

// Design-system button: the slotted label is in the page, the button in the root
await page.getByTestId('ds-control').click();
await page.locator('[data-testid="ds-button"] [slot="label"]').click();

In a real workflow

In the admin, the product editor’s “Available from” field is this date picker, inside a shadow root. Sign in, pick a product, choose a date, save, confirm the dialog, and check that the product list shows the new date.

COR Lens generates locators for shadow DOM, iframes and tables, and grades each one. Click the shadow dom above with it and compare its pick with yours.

Add to Chrome