如何使用硒将idom放入阴影dom内 [英] How to swith to iframe inside shadow dom with selenium

查看:57
本文介绍了如何使用硒将idom放入阴影dom内的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

被测试的应用程序基于Electron(9.1.1版),该软件被编写为Linux的桌面应用程序.电子中有自定义标签< webview> ,其引号为


UPD 我正在通过裸露的端口连接到电子应用,也许这是一种问题?

 公共WebDriver createDriver(DesiredCapabilities wantedCapabilities)ChromeOptions选项=新的ChromeOptions();options.setExperimentalOption("debuggerAddress","localhost:8315");options.setAcceptInsecureCerts(true);options.merge(desiredCapabilities);返回新的ChromeDriver(options);} 

解决方案

实际上,答案很简单.您只需要明确声明要让Webview出现在窗口句柄列表中即可.

这是官方chromedriver文档链接,功能名称为windowTypes

在Java代码中,它将看起来像这样

  ChromeOptions选项=新的ChromeOptions();List< String>webview = Collections.singletonList("webview");options.setExperimentalOption("windowTypes",webview);WebDriver驱动程序=新的C​​hromeDriver(选项);//之后,您可以切换到driver.switchTo().window("yourHandle");//并像往常一样使用它driver.findElement(By.xpath(".//label [text()='Columns']"))); 

Application under test is based on Electron (version 9.1.1) written as desktop application for Linux. In electron there are custom tag <webview> that is quote "The webview tag is essentially a custom element using shadow DOM to wrap an iframe element inside it."

I can access shadow dom and get iframe as WebElement out of it with Java selenim(version 3.141.59).

But swithcing to iframe still left me on parent context.

And my question is:

HOW TO SWITH TO IFRAME INSIDE SHADOW DOM?

//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']"));


This is HTML of the page, and I can get html of webview executing in devtools command document.querySelector('webview').openDevTools(); That's why I'm sure that .//label[text()='Columns'] exist.


UPD I'm connecting to electron application through exposed port, maybe this is kind of issue?

    public WebDriver createDriver(DesiredCapabilities desiredCapabilities) 

        ChromeOptions options = new ChromeOptions();
        options.setExperimentalOption("debuggerAddress", "localhost:8315");
        options.setAcceptInsecureCerts(true);
        options.merge(desiredCapabilities);

        return new ChromeDriver(options);
    }

解决方案

Actually answer was pretty simple. You need just explicitly declare that you want webview to appear in the list of window handles.

Here is official chromedriver docs link and capabilities name is windowTypes

In java code it will be looked like this

ChromeOptions options = new ChromeOptions();
List<String> webview = Collections.singletonList("webview");
options.setExperimentalOption("windowTypes", webview);

WebDriver driver = new ChromeDriver(options);

//after that you can just switch to is
driver.switchTo().window("yourHandle");

//and work with it as usual
driver.findElement(By.xpath(".//label[text()='Columns']"));

这篇关于如何使用硒将idom放入阴影dom内的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆