简体   繁体   English

如何使用Selenium Webdriver处理HTML Shadow Dom?

[英]How to handle html shadow dom using selenium webdriver?

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

WebElement root1= driver.findElement(By.xpath("(//div[@id='accordionSearch']//descendant::input[@class='form-control'])[1]"));
WebElement shadowRoot1=expandShadowRootElement(root1);
WebElement element= shadowRoot1.findElement(By.cssSelector("div[id=editing-view-port]/div"));
element.click();

This code is not working. 该代码不起作用。

在此处输入图片说明 在此处输入图片说明

I was facing similar problem recently and the only solution that worked for me was javascript. 最近我遇到了类似的问题,唯一对我有用的解决方案是javascript。 This is the example of Java code that I used. 这是我使用的Java代码示例。 It gets <div id="details"> from the first element in Chrome's download page and saves it as WebElement. 它从Chrome的下载页面的第一个元素中获取<div id="details"> ,并将其另存为WebElement。

WebElement details = (WebElement) ((JavascriptExecutor) driver).
executeScript("return document.querySelector('downloads-manager').
shadowRoot.querySelector('#downloads-list downloads-item').
shadowRoot.querySelector('#content #details')");

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM