Skip to content

Shadow DOM Support

Modern web applications (especially those built with Web Components) often use Shadow DOM to encapsulate styles and markup.

The Challenge

Elements inside a real shadow root are isolated from the main document. XPath cannot see them, and a CSS selector cannot start in one tree and continue in another.

The QWeb Solution

With Shadow DOM support enabled, Text, Item and CSS locators search the main document and every open shadow root, including nested ones. XPath does not. Turn the support on explicitly, because it changes the search scope.

Enabling Support

Use SetConfig to turn on Shadow DOM traversal.

*** Test Cases ***
Interact with Shadow DOM
    # Enable Shadow DOM support
    SetConfig      ShadowDOM    True

    OpenBrowser    chrome://settings    chrome

    # Now QWeb can see elements inside shadow roots
    VerifyText     Clear browsing data

What can search inside a shadow root

  • Text keywords, such as ClickText and VerifyText.
  • Item keywords, such as ClickItem and VerifyItem.
  • css\= locators, including on input keywords such as TypeText.
  • XPath does not find elements inside a real shadow root. It does see Lightning synthetic-shadow content.
ClickText      Save Settings
ClickElement   css\=button#save
# Does not find a button that is inside a shadow root
ClickElement   xpath\=//button[@id\='save']

Synthetic shadow

Salesforce Lightning synthetic shadow is not a real shadow root. The component markup stays in the document, and the page replaces querySelectorAll so a normal CSS search cannot see it. XPath never uses that function, so it still finds the markup.

QWeb checks whether querySelectorAll is still the browser's own function. When the page has replaced it, a css\= locator searches that same document tree and finds the same elements as XPath, with ShadowDOM left off. A selector can cross a synthetic component, because the markup is in one document. On any other page the function is unchanged, and css\= stays a normal CSS search.

A real open shadow root, including one inside a Lightning page, is separate from that tree. It still needs ShadowDOM on. The check does not turn that traversal on by itself.

Telling them apart

Judge the element you want to click, not the whole page. A Lightning page can contain both.

In Chrome's Elements panel, a native shadow root is labeled #shadow-root (open). Synthetic component markup is ordinary DOM and has no #shadow-root entry.

An XPath locator is the same test from a script. If xpath\= finds the element, it is not inside a native shadow root. If XPath fails and css\= finds it only after SetConfig ShadowDOM True, it is inside a native open shadow root.

element.shadowRoot and instanceof ShadowRoot do not distinguish them on Lightning. Synthetic shadow replaces ShadowRoot, so a stand-in root passes that check too.

CSS locators

The selector has to match inside one tree: the light DOM, or a single open shadow root. It can reach nested shadow roots, but it cannot start outside a root and continue inside it. Shadow-piercing syntax such as >> and >>> is not supported, and neither are closed shadow roots, ::part() or ::slotted().

Text filters work the same way as elsewhere. See Locator Strategies for the selector syntax.

SetConfig       ShadowDOM    True
# .actions and the button are in the same shadow root
ClickElement    css\=.actions button:text-is("Save")
# settings-panel is the shadow host; .actions is inside its shadow root
ClickElement    css\=settings-panel .actions button

Matches in the main document come first, then matches in shadow roots in document order. index counts in that order.