0

I want to get shadow element using text "ShadowRootLabel" from below code :

<div id="example">
#shadow-root
<div id="root" part="root">
  <div id="label" part="label">ShadowRootLabel</div>
</div>
</ptcs-label>
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352

1 Answers1

0

To identify the element using the text ShadowRootLabel using Selenium driven WebDriver you can use the following based solution:

  • Code Block:

    WebElement root = driver.findElement(By.cssSelector("div#example"));
    WebElement shadow_root = expand_shadow_element(root);
    WebElement ShadowRootLabel = shadow_root.findElement(By.xpath("//div[@id='root']/div[text()='ShadowRootLabel']"));
    
  • expand_shadow_element() method:

    public static WebElement expand_shadow_element(WebElement element)
    {
        WebElement shadowRoot = (WebElement)((JavascriptExecutor)driver).executeScript("return arguments[0].shadowRoot", element);
        return shadowRoot;
    }
    

References

You can find a couple of relevant discussions in:

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352