Shadow DOM creates a separate DOM tree that standard Selenium locators cannot access directly. To interact with Shadow DOM elements, Selenium must execute JavaScript to get the shadow root and then find elements inside it.
WebElement host = driver.findElement(By.cssSelector("my-shadow-host"));Option A uses JavaScriptExecutor to return the shadowRoot from the shadow host element, then finds the button inside it. This is the correct way to access Shadow DOM elements in Selenium Java.
Selenium cannot pierce Shadow DOM boundaries with normal CSS selectors. It will not find elements inside Shadow DOM and throws NoSuchElementException.
Custom elements like shadow hosts use their tag name as the selector. So 'custom-widget' selects the element <custom-widget>.
To make Shadow DOM content accessible, developers should use proper ARIA roles and labels inside the Shadow DOM. Also, an open shadow root allows assistive technologies to access the content.