Python-Selenium: 如何切换到位于shadow DOM内部的 ‘switch_to.active_element’ 输入元素?

huangapple go评论73阅读模式
英文:

Python-Selenium: How to 'switch_to.active_element' to an input element inside a shadow dom?

问题

我正在使用Python和Selenium编写一个QA自动化脚本。

我想检查当网页渲染时输入是否已经聚焦。 我的问题是,我找不到一种方法来获取位于影子DOM内的当前聚焦输入。

HTML文档如下:

<div>
  #shadow-root
    #此输入元素在页面渲染时已经聚焦
    <input type='text'>
</div>

我尝试过driver.switch_to.active_element,但它始终返回None,可能是因为它无法访问影子DOM。

我希望有人知道另一种获取影子DOM内当前聚焦输入元素的方法。

英文:

I am writing a QA automation script using Python and Selenium.

I want to check if the input is already focused when the webpage is rendered.
My problem is that I can't find a way to get the currently focused input that is inside the shadow dom.

HTML doc looks like this:

&lt;div&gt;
  #shadow-root
    # this input element is already focused when the page is rendered
    &lt;input type=&#39;text&#39;&gt;
&lt;/div&gt;

I've tried driver.switch_to.active_element but it always returns None, probably because it can't access shadow dom.

I hope anyone knows another way to get the currently focused input element inside a shadow dom

答案1

得分: 0

当网页被渲染后,您会观察到&lt;input&gt;已经被聚焦,但Selenium的焦点在_顶层浏览上下文_。

因此,要识别&lt;input&gt;元素,您需要首先将Selenium的焦点置于影子根内。

> 您可以在PrintText-如何使用Selenium Python在#shadow-root(open)中提取信息中找到相关的详细讨论。

英文:

When the webpage is rendered though you observe that the &lt;input&gt; is already focused but Selenium's focus is at the Top Level Browsing Context.

So to identify the &lt;input&gt; element you need to bring Selenium's focus within the shadow-root first.

>You can find a relevant detailed discussion in PrintText-How to extract info within a #shadow-root (open) using Selenium Python

huangapple
  • 本文由 发表于 2023年2月18日 15:26:09
  • 转载请务必保留本文链接:https://go.coder-hub.com/75491825.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定