Hello,
Please find the below code which access the shadow root:
Steps:
1) First locate the parent tag of shadow root
2) Use that parentOfShadowRoot element to access the tags inside it
3) Use elementInsideShadowRoot instance inorder to access the all tags that comes under root
//ds header is class inside which I have shadow root element
WebElement parentOfShadowRoot = driver.findElement(By.xpath("//ds-header"));
System.out.println(parentOfShadowRoot.getAttribute("id")); // returning __nuxt
//locating .ds-logo which is inside shadow root
WebElement elementInsideShadowRoot = (WebElement) ((JavascriptExecutor) driver)
.executeScript("return arguments[0].shadowRoot.querySelector('.ds-logo')", parentOfShadowRoot);
if (elementInsideShadowRoot != null) {
//performing action to test
String val = elementInsideShadowRoot.getAttribute("class");
System.out.println(val);
} else {
System.out.println("Element inside shadow root not found.");
}
Thanks!