Accessing elements in the shadow DOM

There’s no way to access the shadow root of native HTML 5 elements.

Not useful in this case, but with Chrome it’s possible to access a custom created shadow root:

var root = document.querySelector("#test_button").createShadowRoot();
root.innerHTML = "<button id='inner_button'>Button in button</button"
<button id="test_button"></button>

The root can then be accessed this way:

 var element = document.querySelector("#test_button").shadowRoot;

If you want to automate a click on the inner button with selenium python (chromedriver version 2.14+):

 >>> outer = driver.execute_script('return document.querySelector("#test_button").shadowRoot')
 >>> inner = outer.find_element_by_id("inner_button")
 >>> inner.click()

Update 9 Jun 2015

This is the link to the current Shadow DOM W3C Editor’s draft on github:

http://w3c.github.io/webcomponents/spec/shadow/

If you’re interested in browsing the blink source code, this is a good starting point.

Leave a Comment