Home > Backend Development > Python Tutorial > How to Extract Product Information from Shadow-Root Elements Using Selenium Python?

How to Extract Product Information from Shadow-Root Elements Using Selenium Python?

Susan Sarandon
Release: 2024-10-19 06:40:02
Original
458 people have browsed it

How to Extract Product Information from Shadow-Root Elements Using Selenium Python?

Extracting Information from Shadow-Root Elements Using Selenium Python

In this post, we address the issue of extracting product information from the website https://www.tiendasjumbo.co/buscar?q=mani. These elements are placed within a #shadow-root (open) element, making conventional extraction methods ineffective.

Understanding the Shadow-Root

Shadow-root is a technique used to encapsulate DOM elements, hiding them from the main HTML document. To access elements within a shadow-root, specific shadow-root locators must be utilized.

Solution: Using ShadowRoot.querySelector()

To extract the product label, we implement the following strategy:

  1. Access the Shadow-Root: Obtain the impulse-search element and access its shadow-root.
  2. Use shadowRoot.querySelector(): Navigate within the shadow-root and locate the desired element using the selector. In this case, it's the product label.

Code Example:

<code class="python">from selenium import webdriver
from random import randint

driver = webdriver.Firefox(executable_path="C:\Program Files (x86)\geckodriver.exe")
time.sleep(4)

url = "https://www.tiendasjumbo.co/buscar?q=mani"
driver.maximize_window()
driver.get(url)

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>
Copy after login

This code will print the product label for the provided URL.

Additional Notes:

  • For Microsoft Edge and Google Chrome version 96, specific techniques are required to handle shadow-root access. Consult the provided references for further information.
  • References have been included for detailed discussions and examples.

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

source:php
Statement of this Website
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
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template