CodexBloom - Programming Q&A Platform

Selenium WebDriver scenarios to extract text from a shadow DOM element in a Polymer web application with Chrome 120

👀 Views: 382 đŸ’Ŧ Answers: 1 📅 Created: 2025-08-08
selenium webdriver chromedriver shadow-dom polymer Python

I'm working through a tutorial and I've been struggling with this for a few days now and could really use some help... I'm trying to extract text from a shadow DOM element in a Polymer-based web application using Selenium WebDriver with ChromeDriver version 120. However, when I attempt to locate the element, I receive a `NoSuchElementException`. I've verified that the element is indeed present within the shadow DOM, but it seems that Selenium is unable to access it directly. Here's a simplified version of what I'm trying to do: ```python from selenium import webdriver from selenium.webdriver.chrome.service import Service from selenium.webdriver.common.by import By from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.support import expected_conditions as EC from selenium.webdriver.chrome.options import Options chrome_options = Options() service = Service('path/to/chromedriver') driver = webdriver.Chrome(service=service, options=chrome_options) driver.get('https://example.com') # Wait for the shadow host element to be available shadow_host = WebDriverWait(driver, 10).until( EC.presence_of_element_located((By.CSS_SELECTOR, 'my-shadow-host')) ) # Attempt to access the shadow root shadow_root = driver.execute_script('return arguments[0].shadowRoot', shadow_host) # Try to locate the target element within the shadow DOM shadow_element = shadow_root.find_element(By.CSS_SELECTOR, 'my-shadow-element') text = shadow_element.text print(text) ``` I expected this code to successfully print the text contained within `my-shadow-element`, but instead, I get the following behavior: ``` selenium.common.exceptions.NoSuchElementException: Message: Unable to locate element: my-shadow-element ``` I've tried various approaches, including using different selectors, but nothing seems to work. Additionally, I've checked the browser console, and the element is available when I inspect it. Is there a specific way to handle shadow DOM elements in Selenium that I'm missing? Any insights or suggestions would be greatly appreciated! How would you solve this? This is part of a larger service I'm building. Am I missing something obvious? My development environment is Debian. I'd love to hear your thoughts on this.