Home >Backend Development >Python Tutorial >How to Extract Shadow DOM Elements Using Selenium and Python?

How to Extract Shadow DOM Elements Using Selenium and Python?

Patricia Arquette
Patricia ArquetteOriginal
2024-10-19 06:42:30363browse

How to Extract Shadow DOM Elements Using Selenium and Python?

Shadow DOM Extraction Using Selenium Python

Within the online store URL provided, the product information exists within a #shadow-root (open) element. To access this data using Selenium Python, we need to utilize the shadowRoot.querySelector() method.

Solution:

To retrieve the product label, employ the following code:

<code class="python">driver.get('https://www.tiendasjumbo.co/buscar?q=mani')
item = driver.execute_script("return document.querySelector('impulse-search').shadowRoot.querySelector('div.group-name-brand h1.impulse-title span.formatted-text')")
print(item.text)</code>

This script navigates to the specified URL and executes JavaScript to access the element within the shadow DOM.

Reference:

Refer to the following discussions for further insights:

  • 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

Note for Microsoft Edge and Google Chrome Version 96:

Chrome v96 has modified the shadow root return values for Selenium. Utilize the provided resources for guidance on this specific version.

The above is the detailed content of How to Extract Shadow DOM Elements Using Selenium and Python?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn