Shadow DOM

Web Automationadvanced

// Definition

An encapsulated DOM subtree attached to an element, whose internals are hidden from the main document's normal selectors — the technology behind web components. A `<custom-widget>` may contain a whole UI inside its shadow root that `document.querySelector` can't reach without explicitly crossing the boundary.

// Why it matters

Shadow DOM is a classic automation blocker — tests fail to find elements that are visibly right there, because they're sealed inside a shadow root. QA cares because more component libraries (and design systems) ship as web components, so knowing how to pierce the shadow boundary is increasingly necessary rather than exotic.

// How to test

// Normal selectors stop at the shadow boundary — opt in to cross it:
cy.get('custom-date-picker')         // the host element
  .shadow()                          // enter its shadow root
  .find('[data-cy=day-15]')          // now reachable
  .click()
// Playwright pierces open shadow roots automatically with its default engine.

// Common mistakes

  • Trying normal selectors and concluding the element "isn't there"
  • Assuming all shadow roots are pierceable (closed shadow roots are not)
  • Not knowing a component uses shadow DOM until the locator mysteriously fails

// Related terms