访问shadow DOM树

时间:2018-06-13 13:13:04

标签: java selenium shadow-dom

无法访问影子dom内的元素。

屏幕截图演示它的外观:
Screenshot demonstrating how it looks

我试图使用:

    WebElement root1 = DriverFactory.driver.findElement(By.tagName("downloads-manager"));       
    WebElement shadowRoot1 = expandRootElement(root1);

    WebElement root2 = shadowRoot1.findElement(By.cssSelector("iron-list"));
    WebElement shadowRoot2 = expandRootElement(root2);

    WebElement root3 = shadowRoot2.findElement(By.cssSelector("downloads-item"));
    WebElement shadowRoot3 = expandRootElement(root3);

    shadowRoot3.findElement(By.xpath("//div[@id='title-area' and contains(.,'Lviv')]/ancestor::div[@class='controls']/a[@id='preview']")).click();
 public WebElement expandRootElement(WebElement element) {
    WebElement ele = (WebElement) ((JavascriptExecutor) DriverFactory.driver)                
    .executeScript("return arguments[0].shadowRoot",element);
    return ele;}

结果:

org.openqa.selenium.NoSuchElementException: no such element: Unable to locate element: {"method":"css selector","selector":"downloads-item"}

2 个答案:

答案 0 :(得分:0)

你写错了请参考下面的代码

By.tagName("铁列表&#34))  By.cssselectorBy.tagName("#下载项目&#34));

答案 1 :(得分:0)

您可以在Xpath下面使用

.//*[@id='downloads-list']/downloads-item

或CSS选择器

#downloads-list>downloads-item