【发布时间】:2020-11-23 06:37:48
【问题描述】:
测试中的应用程序基于 Electron(版本 9.1.1)编写为 Linux 桌面应用程序。
在电子中有自定义标签<webview>,即引用"The webview tag is essentially a custom element using shadow DOM to wrap an iframe element inside it."
我可以使用 Java selenim(版本 3.141.59)访问 shadow dom 并以 WebElement 的形式获取 iframe。
但是切换到 iframe 仍然让我处于父上下文中。
我的问题是:
如何在 SHADOW DOM 内切换到 IFRAME?
//getting webdriver
WebDriver driver = WebDriverRunner.getWebDriver();
JavascriptExecutor js = (JavascriptExecutor) driver;
//acquire shadow dom WebElement
WebElement shadowDom = (WebElement) js.executeScript("return arguments[0].shadowRoot", driver.findElement(By.tagName("webview")));
//acquire iframe WebElement
WebElement iframe = shadowDom.findElement(By.tagName("iframe"));
//trying to swith to iframe inside shadow DOM, but still at parent context because can't find element that exist in iframe
driver.switchTo().frame(iframe);
//obviously produce NoSuchElementException
driver.findElement(By.xpath(".//label[text()='Columns']"));
这是页面的 HTML,我可以在 devtools 命令 document.querySelector('webview').openDevTools(); 中执行 webview 的 html,这就是为什么我确定 .//label[text()='Columns'] 存在。
UPD我正在通过暴露端口连接到电子应用程序,也许这是一种问题?
public WebDriver createDriver(DesiredCapabilities desiredCapabilities)
ChromeOptions options = new ChromeOptions();
options.setExperimentalOption("debuggerAddress", "localhost:8315");
options.setAcceptInsecureCerts(true);
options.merge(desiredCapabilities);
return new ChromeDriver(options);
}
【问题讨论】:
标签: selenium iframe webview webdriver shadow-dom