英文:
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:
<div>
#shadow-root
# this input element is already focused when the page is rendered
<input type='text'>
</div>
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
当网页被渲染后,您会观察到<input>
已经被聚焦,但Selenium的焦点在_顶层浏览上下文_。
因此,要识别<input>
元素,您需要首先将Selenium的焦点置于影子根内。
> 您可以在PrintText-如何使用Selenium Python在#shadow-root(open)中提取信息中找到相关的详细讨论。
英文:
When the webpage is rendered though you observe that the <input>
is already focused but Selenium's focus is at the Top Level Browsing Context.
So to identify the <input>
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
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论