Shadow-dom support for selenium

Arav picture Arav · Mar 21, 2016 · Viewed 12.2k times · Source

I am working on an automation project which uses shadow dom extensively. So every time using execute_script function to access shadow root

for eg.

root = driver.execute_script('return document.querySelector(".flex.vertical.layout").shadowRoot')

Then use the root to access the elements within. Since we have shadow roots at many levels, this is annoying me a lot. is there any better solution exist than this to access elements within shadow root?

I am using Chrome 2.20 driver.

Answer

Arav picture Arav · Mar 22, 2016

By googling I found another workaround for this problem - which is using The "/deep/ combinator".

for example, I was able to access all the shadow roots elements by

driver.find_elements_by_css_selector('body/deep/.layout.horizontal.center')

This will have access to the element with compound class name "layout horizontal center" regardless of the number of shadow roots it has.

But this only working in chrome driver and I see notes "/deep/" is a deprecated approach.