无法使用 Python Selenium 在 shadow-root(打开)中定位元素 [英] Can't locate elments within shadow-root (open) using Python Selenium

查看:284
本文介绍了无法使用 Python Selenium 在 shadow-root(打开)中定位元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从 .


解决方案

要提取文本 Microsoft Windows,您必须使用 shadowRoot.querySelector() 你可以使用下面的定位器策略:

  • 代码块:

    driver.get('https://www.virustotal.com/gui/file/03d1316407796b32c03f17f819cca5bede2b0504ecdb7ba3b845c1ed618ae934/details')print(driver.execute_script("return document.querySelector('file-view').shadowRoot.querySelector('vt-ui-file-details').shadowRoot.querySelector('vt-ui-signature-info').shadowRoot.querySelector('vt-ui-expandable-detail').shadowRoot.querySelector('slot')").text)

  • 控制台输出:

    微软视窗


参考文献

您可以在以下位置找到一些相关讨论:

I'm trying to get the content under Signers, Counter Signers and X509 Signers from https://www.virustotal.com/gui/file/03d1316407796b32c03f17f819cca5bede2b0504ecdb7ba3b845c1ed618ae934/details

from selenium import webdriver
op = webdriver.ChromeOptions()
op.add_argument('headless')
driver = webdriver.Chrome(executable_path="/Desktop/chromedriver", options=op)
details_url = "https://www.virustotal.com/gui/file/03d1316407796b32c03f17f819cca5bede2b0504ecdb7ba3b845c1ed618ae934/details"

driver.get(details_url)
element = driver.find_element_by_xpath("/html/body/vt-ui-shell")
print(element.text)

The result doesn't include the parts under Signers, Counter Signers and X509 Signers

I also tried to do

driver.find_element_by_xpath("//*[@id="details"]//div/vt-ui-signature-info//vt-ui-expandable/span")

to locate that part, but it ended up giving me

NoSuchElementException: Message: no such element: Unable to locate element

解决方案

The Signers fields e.g. Microsoft Windows are within nested #shadow-root (open).


Solution

To extract the text Microsoft Windows you have to use shadowRoot.querySelector() and you can use the following Locator Strategy:

  • Code Block:

    driver.get('https://www.virustotal.com/gui/file/03d1316407796b32c03f17f819cca5bede2b0504ecdb7ba3b845c1ed618ae934/details')
    print(driver.execute_script("return document.querySelector('file-view').shadowRoot.querySelector('vt-ui-file-details').shadowRoot.querySelector('vt-ui-signature-info').shadowRoot.querySelector('vt-ui-expandable-detail').shadowRoot.querySelector('slot')").text)
    

  • Console Output:

    Microsoft Windows
    


References

You can find a couple of relevant discussions in:

这篇关于无法使用 Python Selenium 在 shadow-root(打开)中定位元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆