Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Find the element through its shadow host, then call Selenium’s element screenshot API on the resulting WebElement. In Python, use target.screenshot_as_png for PNG bytes or target.screenshot('/absolute/path/target.png') to save a PNG file. Shadow-root lookup requires Selenium 4 or greater.

Find the element inside its shadow DOM

A shadow DOM is an encapsulated DOM tree attached to a host element. Selenium’s documented lookup pattern is to locate the host from the driver, get its shadow root, then search for the target from that root—not from the top-level driver. See Selenium’s element finder guide.

from selenium.webdriver.common.by import By

host = driver.find_element(By.CSS_SELECTOR, "my-component")
root = host.shadow_root
target = root.find_element(By.CSS_SELECTOR, ".target")

Replace my-component and .target with selectors matching the page. The driver must already be an active Selenium WebDriver session with the page loaded.

For nested shadow roots

Repeat the host-to-root lookup at each shadow boundary. Find the inner host from the outer root, get that host’s shadow_root, and search within the new root for the target.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
outer_host = driver.find_element(By.CSS_SELECTOR, "outer-component")
outer_root = outer_host.shadow_root
inner_host = outer_root.find_element(By.CSS_SELECTOR, "inner-component")
inner_root = inner_host.shadow_root
target = inner_root.find_element(By.CSS_SELECTOR, ".target")

The Python ShadowRoot API provides descendant lookup scoped to a root.

Capture a screenshot of the target element

Once located, the target is a Selenium WebElement. Call its screenshot method to save a PNG, or read its PNG bytes for an in-memory workflow. The Python WebElement API documents both choices.

Rank #2
Sale
HTML and CSS: Design and Build Websites
  • HTML CSS Design and Build Web Sites
  • Comes with secure packaging
  • It can be a gift option
# Save the element screenshot as a PNG file.
target.screenshot("/absolute/path/target.png")

# Or keep the PNG data in memory.
png_bytes = target.screenshot_as_png

Use an absolute path when saving to make the output location explicit. For example, pass png_bytes to code that uploads or processes image bytes; use the file method when the next step expects a file on disk. Both are element screenshots, not a screenshot of the shadow root as a whole.

Complete example with a WebDriver session

This runnable pattern includes driver setup and cleanup. Install Selenium and the browser/driver setup required by your environment, then substitute a real page URL and the page’s host and descendant selectors.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
from selenium import webdriver
from selenium.webdriver.common.by import By

url = "https://example.com"

with webdriver.Chrome() as driver:
    driver.get(url)

    host = driver.find_element(By.CSS_SELECTOR, "my-component")
    root = host.shadow_root
    target = root.find_element(By.CSS_SELECTOR, ".target")

    target.screenshot("/absolute/path/target.png")
    # Alternative: png_bytes = target.screenshot_as_png

The WebDriver session must be able to load the page and locate the element before capture. The element screenshot request maps to WebDriver’s element screenshot endpoint, as shown in Selenium’s Python remote connection API.

Browser and Selenium compatibility

Selenium’s finder documentation says the shadow-root methods require Selenium 4 or greater. The Python shadow_root API reference names Chromium, Firefox, and Safari. That does not establish a version-by-version guarantee for this exact end-to-end screenshot flow, so check the Selenium, browser, and driver versions installed in the environment where you run it.

Rank #4
Sale
Web Design with HTML, CSS, JavaScript and jQuery Set
  • Brand: Wiley
  • Set of 2 Volumes
  • A handy two-book set that uniquely combines related technologies Highly visual format and accessible language makes these books highly effective learning tools Perfect for beginning web designers and front-end developers

Troubleshoot lookup and capture failures

No shadow root is attached

If reading host.shadow_root raises NoSuchShadowRoot, verify that the selected host is the intended element and that the page has attached a shadow root to it. Selenium documents this exception in the WebElement API.

The target selector is not found

  • Confirm the selector matches an element inside the shadow tree, not the host or an element elsewhere in the page.
  • Search from the correct root. A driver-level lookup does not replace lookup scoped to the host’s shadow root.
  • For nested components, get each intermediate host’s shadow root before searching for the next descendant.

The file is missing after the call

Use a valid absolute path and ensure the process can write to its destination. If the next step can consume bytes directly, use screenshot_as_png instead of writing a file.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

Or skip the browser setup

For a URL-level capture rather than Selenium automation of a shadow-DOM descendant, ScreenshotNeo provides a screenshot API and MCP server. It does not replace Selenium’s root-scoped lookup when you need to target a particular element inside a shadow tree.

One GET request returns an image or PDF. This cURL example saves a WebP screenshot of Stripe:

curl -G "https://api.screenshotneo.com/v1/shot" -d access_key=YOUR_API_KEY --data-urlencode url=https://stripe.com -o shot.webp

See the ScreenshotNeo documentation for API parameters. Cookie banners are accepted and removed before capture, along with supported newsletter popups and chat widgets; those steps can be turned off. Bot checks, blank pages, timeouts, failed loads, and cache hits are not billed, and response headers identify the page verdict and billing status. Its MCP server offers take_screenshot, get_page_info, and capture_pdf for AI agents. The Free plan includes 1,000 shots per month with no card; paid plans start at $5 for 3,000 shots.

Sign up for ScreenshotNeo’s free plan to get 1,000 screenshots a month with no card.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Frequently Asked Questions

Does Selenium screenshot the shadow root itself?

No. The documented approach is to locate the desired descendant as a WebElement and call that element’s screenshot API.

Which output should I choose: screenshot_as_png or screenshot(path)?

Use screenshot_as_png when the next step needs PNG bytes in memory; use screenshot(path) when you need a PNG file.

Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.