简体   繁体   English

如何阅读#shadow-root(用户代理)下的文本

[英]How to read text that is under #shadow-root (user-agent)

I am using Selenium (Python) for automating a web page.我正在使用 Selenium (Python) 来自动化网页。 I am trying to get text from an input field that is under #shadow-root (user-agent).我正在尝试从 #shadow-root (user-agent) 下的输入字段中获取文本。 Xpath I used:我使用的 Xpath:

driver.find_element_by_xpath("**//*/p-calendar/span/input**").text

is not returning anything.没有返回任何东西。 Attached the screenshot of my DOM element.附上我的 DOM 元素的屏幕截图。 Requirement: To get text from the shadow root : 01:01要求:从影子根获取文本:01:01

#shadow-root(用户代理)

You can use driver.execute_script to inject JavaScript code that returns the ShadowRoot then use find_element to get the children element of the shadow root you are looking for.您可以使用driver.execute_script注入返回 ShadowRoot 的 JavaScript 代码,然后使用find_element获取您正在寻找的影子根的子元素。

input_shadow = driver.execute_script('''return document.querySelector("$1").shadowRoot''')
div_text = inputShadow.find_element_by_tag_name("div").text

$1 - Your element's identifier or selector . $1 - Your element's identifier or selector

If you are keen to using xpath to find elements如果您热衷于使用 xpath 查找元素

input_shadow = driver.execute_script('''return $x(\"//*/p-calendar/span/input\")[0]''')
div_text = inputShadow.find_element_by_tag_name("div").text

As per @hayatoito 's (creator of Shadow DOM) comment :根据@hayatoito (Shadow DOM 的创建者)的评论

The original motivation of introducing a closed shadow tree is "Never allow an access to a node in a closed shadow tree, via any APIs, from outside" , AFAIK.引入封闭影子树的最初动机是"Never allow an access to a node in a closed shadow tree, via any APIs, from outside" ,AFAIK。 Like that we can not access a node in the internal hidden shadow tree which is used in <video> element, in Blink.像这样,我们无法访问 Blink 中<video>元素中使用的内部隐藏阴影树中的节点。

In fact, I designed a closed shadow tree in such a way.其实我就是这样设计了一个封闭的影子树的。 If there is a way to access a node in a closed shadow tree, it should be considered as a bug of the spec.如果有一种方法可以访问封闭阴影树中的节点,则应将其视为规范的错误。

I think it's totally okay to have an API to allow an access in the layer of Chrome apps or extensions.我认为拥有一个 API 来允许在 Chrome 应用程序或扩展程序层中进行访问是完全可以的。 However, for a normal web app, I think the current agreement is "Never allow it" .但是,对于普通的网络应用程序,我认为当前的协议是"Never allow it"

If we allowed it, that means we do not need a closed shadow tree.如果我们允许,这意味着我们不需要封闭的影子树。 Just having an open shadow tree is enough, I think.我认为只要有一棵开放的影子树就足够了。

Furhter @Supersharp in his comment below his own answer within the discussion How to get element in user-agent shadow root with JavaScript? Furhter @Supersharp在他自己的回答下面的评论中,在讨论中如何使用 JavaScript 获取用户代理影子根中的元素? mentions:提到:

#shadow-root (user-agent) are browser vendors native implementation so they are not documented and will never be accessible. #shadow-root (user-agent)是浏览器供应商的本地实现,因此它们没有记录并且永远无法访问。 Only open Shadow DOM are, as per the specs根据规范,只有开放的 Shadow DOM


WebDriver perspective WebDriver 视角

Recently, @AutomatedTester [David Burns, Chief Bacon Officer, Mozilla Corporation] initiated a discussion on WebDriver - Testability of web components近日,@AutomatedTester [David Burns,Mozilla Corporation 首席培根官]发起了一场关于WebDriver - Web 组件的可测试性的讨论

Currently Selenium Team is open for accepting pull requests for the same.目前, Selenium 团队开放接受相同的拉取请求。


Reference参考

You can find a couple of relevant detailed discussions in:您可以在以下位置找到一些相关的详细讨论:


Outro奥特罗

Here you can find a relevant discussion on How to automate shadow DOM elements using selenium?在这里您可以找到有关如何使用 selenium 自动化 shadow DOM 元素的相关讨论

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

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