简体   繁体   English

无法使用 Python Selenium 在 shadow-root(打开)中定位元素

[英]Can't locate elments within shadow-root (open) using Python Selenium

I'm trying to get the content under Signers, Counter Signers and X509 Signers from https://www.virustotal.com/gui/file/03d1316407796b32c03f17f819cca5bede2b0504ecdb7ba3b845c1ed618ae934/details我正在尝试从https://www.virustotal.com/gui/file/03d1316407796b32c03f17f819cca5bede2b0504ecdb7ba3b845c1ed618ae934/detail获取 Signers、Counter Signers 和 X509 Signers 下的内容

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结果不包括签名者、会签者和 X509 签名者下的部分

结果

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
element=driver.execute_script(
    "return document.querySelector('body file-view').shadowRoot.querySelector('vt-ui-file-details').shadowRoot.querySelector('vt-ui-signature-info').shadowRoot.querySelector('vt-ui-expandable').shadowRoot.querySelector('[class=\"details\"]')")

this prints the signature version information, similarly you have to find the rool and call shadowroot and find the element for other roots这将打印签名版本信息,同样您必须找到 rool 并调用 shadowroot 并找到其他根的元素

https://bitsofco.de/what-is-the-shadow-dom/ https://bitsofco.de/what-is-the-shadow-dom/

Shadow dom is not part of document so you have to use javascript executor to find elements inside it Shadow dom 不是文档的一部分,因此您必须使用 javascript 执行程序来查找其中的元素

The Signers fields eg Microsoft Windows are within nested #shadow-root (open) .签名者字段,例如Microsoft Windows位于嵌套的 #shadow-root (open)中。

影根


Solution解决方案

To extract the text Microsoft Windows you have to use shadowRoot.querySelector() and you can use the following Locator Strategy :要提取文本Microsoft Windows您必须使用shadowRoot.querySelector()并且可以使用以下定位器策略

  • 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:控制台 Output:

     Microsoft Windows

References参考

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

暂无
暂无

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

相关问题 Python Selenium can't find element by xpath within #shadow-root (open) using Selenium and Python - Python Selenium can't find element by xpath within #shadow-root (open) using Selenium and Python 无法使用 Selenium 和 Python 在 #shadow-root(打开)中找到登录元素 - Unable to locate the Sign In element within #shadow-root (open) using Selenium and Python How to locate the First name field within shadow-root (open) within the website https://www.virustotal.com using Selenium and Python - How to locate the First name field within shadow-root (open) within the website https://www.virustotal.com using Selenium and Python 如何使用 Selenium Python 在#shadow-root(打开)中提取信息? - How to extract info within a #shadow-root (open) using Selenium Python? Python Selenium - 如何在#shadow-root(打开)下的textarea中定位和添加数据 - Python Selenium - How to locate and add data in textarea which is under #shadow-root (open) 通过 Python 和 Selenium 在#shadow-root(打开)中单击按钮的最佳方式 - Best way to click a button within #shadow-root (open) via Python and Selenium 如何使用 Selenium 在#shadow-root(打开)中切换到子框架 - How to switch to the child frame within #shadow-root (open) using Selenium 无法使用 Python Selenium 从 shadow-root 内的元素中提取文本 - Unable to pull text from elements within shadow-root using Python Selenium python selenium:无法定位元素,shadow-root内的svg-icon按钮 - python selenium: unable to locate element, svg-icon button inside shadow-root 如何定位在 shadow-root 中找不到的元素 - How to locate element not found in shadow-root
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM