DriversRecommendedOutdated drivers can make a good PC feel brokenScan driver issues before chasing fixes manually.Scan NowOctober DealsAmazon USOctober deal check: compare before you payAmazon US: current deals, useful picks and tech finds.Check DealsPC HealthRecommendedCrashes, freezes, slowdowns? Check your PC nowSpot repairable issues before they interrupt work.Check PC×
Skip to content
World desk4 min

How to Capture a Screenshot of a Shadow DOM Element with Selenium (Python)

Find a Selenium shadow host, locate a descendant through its shadow root, and capture that WebElement as PNG bytes or a file.

Free tools Windows power users keep installed

One-click scans. No signup required.

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

Use Selenium 4 or later to find the element’s shadow host, open its shadow root, locate the descendant from that root, then call the descendant WebElement’s screenshot API. In Python, use target.screenshot_as_png for PNG bytes or target.screenshot('/absolute/path/target.png') to save a PNG file.

Find the element inside the shadow DOM

A shadow DOM is an encapsulated DOM tree attached to a host element. A normal driver-level search does not cross that boundary: first find the host, then search within its shadow root.

As an Amazon Associate I earn from qualifying purchases.

  1. Locate the shadow host from the WebDriver instance, using the page’s actual selector.
  2. Read the host’s shadow_root property.
  3. Call find_element on that root to locate the desired descendant.
  4. Take the screenshot from the descendant WebElement.

The pattern below uses Selenium’s Python bindings and requires Selenium 4 or greater for the documented shadow-root methods. See Selenium’s element-finding guide.

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

Runnable Python example

Replace the URL, host selector, and target selector with values from the page you are testing. The example saves the element screenshot as a PNG:

from selenium import webdriver
from selenium.webdriver.common.by import By

url = "https://example.com"
host_selector = "my-component"
target_selector = ".target"

# Assumes a compatible browser and driver are installed and available.
driver = webdriver.Chrome()
try:
    driver.get(url)

    host = driver.find_element(By.CSS_SELECTOR, host_selector)
    root = host.shadow_root
    target = root.find_element(By.CSS_SELECTOR, target_selector)

    target.screenshot("/absolute/path/target.png")
finally:
    driver.quit()

The absolute output path must be valid and writable on the machine running the test. Selenium’s Python API documents screenshot as saving a PNG and recommends a full path. The Python WebElement API documents the screenshot methods and shadow-root property.

Keep the PNG in memory

If a later step consumes image data directly, use the bytes property instead of writing a file:

png_bytes = target.screenshot_as_png

png_bytes contains binary PNG data. You can pass it to code that accepts bytes or save it using your preferred file or image library. The API documents both this property and the file-saving method; it does not establish a quality or performance advantage for either.

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

Nested shadow roots

If the target is inside a shadow root that is itself inside another shadow root, repeat the host-to-root lookup at each boundary. Each next host is found from the current root, not from the top-level driver.

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")
target.screenshot("/absolute/path/target.png")

The Python ShadowRoot API describes locating descendants within a shadow root.

Which element should you screenshot?

Use the located descendant WebElement representing the visual area you want. Selenium documents an element screenshot operation; the relevant flow is to find the desired descendant inside the root and call its screenshot method. The documented method is not a screenshot method on the ShadowRoot itself. Selenium maps the WebElement operation to WebDriver’s element screenshot endpoint, as shown in its Python remote connection API.

Browser and version compatibility

The Selenium finder guide says the documented shadow-root methods require Selenium 4 or greater. The Python WebElement reference lists Chromium, Firefox, and Safari for shadow_root. That is not a version-by-version guarantee for every browser, driver, and end-to-end element screenshot combination. Confirm compatibility using the exact Selenium, browser, and driver versions deployed by your project.

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

Troubleshooting

  • NoSuchShadowRoot: The selected element may not be a shadow host, or it may not yet have an attached shadow root. Confirm the host selector and page state before reading host.shadow_root; Selenium documents this error when no root is attached.
  • Target lookup fails: Make sure the target selector is searched from the correct root. A descendant inside a shadow tree is not found by searching only from driver; use root.find_element(...). For nested components, repeat the lookup at each root.
  • Host lookup fails: Check that the selector identifies the host in the top-level document and that navigation has reached the expected page before searching.
  • Screenshot file is missing: Verify that the process can write to the specified absolute path and that the filename ends in .png. For an in-memory result, use screenshot_as_png and handle the returned bytes in your own code.
  • Behavior differs by environment: Check the installed Selenium, browser, and driver versions together. The cited API reference identifies browsers for shadow_root, but does not promise identical results for every version combination.
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 normal URL screenshot rather than a Selenium-specific shadow descendant, ScreenshotNeo provides a one-request screenshot API. It cannot target an element inside your page’s shadow root by selector through the method shown here; use Selenium when you need that element-level control.

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 API documentation for request options. ScreenshotNeo accepts cookie and consent banners before capture and removes more than 60 known consent platforms, newsletter popups, and chat widgets; each cleanup step can be turned off. Bot checks and CAPTCHAs, blank pages, timeouts, failed loads, and cache hits cost nothing, and responses identify the page verdict and billing status. Its MCP server provides 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 free for 1,000 screenshots a month, with no card required.

Frequently Asked Questions

Can I capture the screenshot without saving it to disk?

Yes. Read target.screenshot_as_png to get PNG bytes in memory.

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

Does Selenium’s documented shadow-root support include Safari?

The Python WebElement reference lists Safari, along with Chromium and Firefox, for the shadow_root property. Check your particular Selenium, browser, and driver versions for the end-to-end workflow.

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.

Leave a Reply

Your email address will not be published. Required fields are marked *

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

More from the Wire

  1. World desk4 min
    How to Spot an AI Voice Scam Before Sending MoneyDon’t rely on how a caller sounds. Pause, call back through a known number, and verify the emergency with another trusted person before sending money.
  2. Mountain View desk4 min
    Google’s SynthID Detector: How to Check AI-Generated Images, Video and AudioGoogle’s SynthID Detector looks for an embedded watermark in supported images, video and audio. Here is what its results do—and do not—show.
  3. Redmond desk20 min
    How to create a link to File or Folder in Windows 11Windows 11 gives you several ways to point to a file or folder without moving or duplicating it. You can create a desktop shortcut,…
Recommended PC Tool
Recommended PC Tool
Crashes, No Sound, or Screen Glitches?Free driver scan
PC Slower Than It Used to Be?Free scan - under a minute

Two free Windows tools

One Free Minute Could Fix That PC

Before you go - each of these free tools takes about a minute and tackles what quietly slows a Windows PC down.

Special offer. View Outbyte info, uninstall instructions, EULA, and Privacy Policy.