Automating Shadow DOM with Selenium WebDriver

How to drive web elements inside a shadow root on a web page

Zhimin Zhan
4 min readDec 27, 2021

--

A repost of my daughter’s article (her first on Medium) with permission. I added it here for the convenience of my blog subscribers.

This article is included in my “How to in Selenium WebDriver” series.

A Shadow DOM is a self-contained web component in a web page. Google uses shadow DOMs extensively (link to their Shadow DOM introduction). By default, a standard automation driver is unable to driver web elements inside a shadow DOM.

The latest Selenium WebDriver v4 supports Shadow Roots (HTML elements for a shadow DOM). In this article, I will show a quick and easy way to drive web elements inside a Shadow DOM using Selenium WebDriver.

An Example

The task: Add a new list item to the Editable List of the demo Fiddle site.

Screenshot of Luigi Demo Page which we are about to test

A typical Selenium script looks like this:

# add new list element
driver.find_element(:xpath, "//input[@class='add-new-list-item-input']").send_keys("Buy milk and eggs")

--

--

Zhimin Zhan

Test automation & CT coach, author, speaker and award-winning software developer. Help teams succeed with Agile/DevOps by implementing real Continuous Testing.