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:
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!