对 selenium 的 Shadow-dom 支持 [英] Shadow-dom support for selenium
问题描述
我正在开发一个广泛使用影子 DOM 的自动化项目.我使用 execute_script
函数来访问影子根元素.
I am working on an automation project which uses shadow DOMs extensively.
I use the execute_script
function to access shadow root elements.
例如:
root = driver.execute_script('return document.querySelector(".flex.vertical.layout").shadowRoot')
然后我使用根元素来访问其中的元素.由于我们在许多级别都有阴影根元素,这让我很烦恼.有没有更好的解决方案来访问影子根元素中的元素?
Then I use the root element to access the elements within. Since we have shadow root elements at many levels, this is annoying me a lot. Is there any better solution to access elements within shadow root elements?
我正在使用 Chrome 2.20 驱动程序.
I am using Chrome 2.20 driver.
推荐答案
通过谷歌搜索,我找到了解决此问题的另一种解决方法 - 使用/deep/
组合器".
By googling I found another workaround for this problem - which is using the "/deep/
combinator".
例如,我可以使用
driver.find_elements_by_css_selector('body/deep/.layout.horizontal.center')
这将可以访问具有复合类名称 layout Horizontal center
的元素,而不管它有多少阴影根.
This will have access to the element with the compound class name layout horizontal center
regardless of the number of shadow roots it has.
但这仅适用于 chromedriver,而 /deep/
是一种已弃用的方法.
But this only works for the chromedriver and /deep/
is a deprecated approach.
这篇关于对 selenium 的 Shadow-dom 支持的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!