0

i want to fetch the text from shadow element of Dom

http://prntscr.com/e9smzg

I have tried below code but its not working..

 public String ShadowRootElement(String str) {
    WebElement ele = (WebElement) ((JavascriptExecutor) driver).executeScript("return arguments[0].shadowRoot",getElementByXpath(str));
    return ele.findElement(By.xpath("//div[@id='inner-editor']")).getText().toString(); 
    }

Please refer attached screenshot link for html code.

  public String getEmailId(String str){ 
            return ShadowRootElement(Repo.get("ipEmailId"));       
    }
Shrikant
  • 9
  • 2
  • 1
    Possible duplicate of [how get element from shadow root with JavaScript?](http://stackoverflow.com/questions/38701803/how-get-element-from-shadow-root-with-javascript) – Supersharp Feb 17 '17 at 07:56

2 Answers2

0

First of all, the way you call ele.findElement(By.xpath("//div[@id='inner-editor']")).getText().toString(); is troublesome.

To locate elements under shadow root node,By.xpath() won't work. Only By.id() & By.cssSelector() will work as valid locators. Please refer to this post for more details.

Secondly (and unfortunately), I found even if you can locate the node under shadow root, element.getText() method would return an empty string.. Simply put it doesn't work for me either :-(

0

you will not be able to use xpath with shadowroots, since xpath is applied to DOM

Here, you can pull back all the elements, then use css or other to check if text exists, eg (use driver instead of session, since I wrap my driver):

public static String getAllShadowRootsText(DriverSessions session, String rootNode)
{
    String elsText = "";
    try {
        List<SearchContext> sroots = getAllShadowRoots(session, rootNode);
        
        for(SearchContext sroot : sroots){
            // we have to specify the elements with shadowroot children, we cant just get all *
            List<WebElement> els =  sroot.findElements(By.cssSelector(validDomTypes));
            for(WebElement el : els) {
                elsText = elsText + el.getText();
            }
        }
    }
    catch (Exception e) {} // we might want to loop this, pages change and shadow roots move / go stale
    return elsText;
}

    public static List<SearchContext> getAllShadowRoots(DriverSessions session, String rootNode)
{

    String script =  ""         
        + "function getShadowRoots (node, sroots, func) { "
            + "var done = func(node); "
            + "if (done) {return true;} "
            + "if ('shadowRoot' in node && node.shadowRoot) { "
                + "sroots.push(node.shadowRoot); "
                + "var done = getShadowRoots(node.shadowRoot, sroots, func); "
                + "if (done) {return true;} "
            + "} "
            + "node = node.firstChild; "
            + "while (node) { "
            + "var done = getShadowRoots(node, sroots, func); "
                + "if (done) {return true;} "
                + "node = node.nextSibling; "
            + "} "
        + "} "
        + "try { "
            + "sroots = new Array(); "
            + "getShadowRoots("+rootNode+", sroots, function (node, sroots) {}); "
            + "return sroots;"
            + "} "
        + "catch(err){return null};";

    JavascriptExecutor js = (JavascriptExecutor)session.getDriver();
    @SuppressWarnings("unchecked")
    List<SearchContext> els = (List<SearchContext>) js.executeScript(script);
    return els;
}