简体   繁体   English

如何使用 Selenium Java 框架自动化 Shadow DOM

[英]How to automate Shadow DOM using Selenium Java framework

I am using Selenium Java framework for test automation and recently the application was upgraded with Shadow DOM.我正在使用 Selenium Java 框架进行测试自动化,最近该应用程序使用 Shadow DOM 进行了升级。 We have tried to automate using javascript executor querySelector.我们尝试使用 javascript 执行器 querySelector 实现自动化。 The application is having more than 20 shadow roots in a single page and most of the shadow roots are child of the previous root.该应用程序在单个页面中有超过 20 个影子根,并且大多数影子根是前一个根的子级。 So to identify an element we need to traverse through many roots as in document.querySelector('root1').shadowRoot.querySelector('root2')....like wise till nth root to find an element.因此,要识别一个元素,我们需要遍历许多根,如 document.querySelector('root1').shadowRoot.querySelector('root2').... 直到第 n 个根才能找到一个元素。

WebElement DropDown = (WebElement) js.executeScript("return document.querySelector('sn-test-layouts > sn-test-main > sn-test-contents').shadowRoot.querySelector('testroot1').shadowRoot.querySelector('testroot2')");

After finding an element, the normal selenium click or select methods are not working.找到元素后,正常的 selenium click 或 select 方法不起作用。 So am using javascript methods to perform click, enter text etc.所以我使用 javascript 方法来执行点击,输入文本等。

Could you please provide insight on any other solutions to automate Shadow DOM using selenium Java.您能否提供有关使用 selenium Java 自动化 Shadow DOM 的任何其他解决方案的见解。 Thanks in advance提前致谢

Serenity offers a shadow DOM handler, and implementation looks like this: Serenity 提供了一个影子 DOM 处理程序,实现如下所示:

public WebElementFacade getElementInShadowDOM(String shadow, String element) {
    return find(ByExpandedShadowDom.of(shadow))
            .find(By.cssSelector(element));
}

where element is the dropdown you want to access and shadow is the css selector of shadow root.其中element是您要访问的下拉列表, shadow是影子根的 css 选择器。 WebElementFacade is a wrapper for WebElements provided by Serenity that offer many cool and more efficient methods. WebElementFacade 是 Serenity 提供的 WebElements 的包装器,它提供了许多很酷和更有效的方法。

Please note that currently SerenityBDD is not yet up to date with newest chrome and Selenium 4, from what i know the work is in progress.请注意,目前 SerenityBDD 尚未更新最新的 chrome 和 Selenium 4,据我所知工作正在进行中。

It's my first time posting, finally time to give back:)这是我第一次发帖,终于有时间回馈了:)

In Selenium 4.0, for Chromium versions 96+ you can use the getShadowRoot() method, then locate the child element.在 Selenium 4.0 中,对于 Chromium 版本 96+,您可以使用getShadowRoot()方法,然后定位子元素。 This shouldn't have any issues with nesting.这不应该有任何嵌套问题。

For older versions of Chrome or Safari, you need to cast to SearchContext instead of WebElement .对于旧版本的 Chrome 或 Safari,您需要强制转换为SearchContext而不是WebElement

If you're working with Firefox or Selenium 3, there are more complicated workarounds.如果您使用的是 Firefox 或 Selenium 3,则有更复杂的解决方法。 I've written them all out here: https://titusfortner.com/2021/11/22/shadow-dom-selenium.html我把它们都写在这里了: https://titusfortner.com/2021/11/22/shadow-dom-selenium.html

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

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