I took the information from MivaScott's answer and created a recursive method for my solution, I thought it could be of use to other people so here it is. I was using this to click a play button in a video player.
All you need to provide is a string array of the CSS selectors of the shadow-root. The method will return the final shadow root element, so you can add another selector onto the end (in my case, an svg). Please see my example:
The Player's Shadow-Root Structure
public IWebElement PlayButton {
get {
string[] shadowRootSelectors = { "apc-controls", "apc-control-footer", "apc-toggle-play", "apc-icon-play" };
return FindShadowRootElementRecursive(shadowRootSelectors).FindElement(By.CssSelector("svg"));
}
set {
}
}
And the recursive method itself:
public IWebElement FindShadowRootElementRecursive(string[] selectors = null, IWebElement element = null) {
IWebElement root = null;
IWebElement selectorElement = null;
bool baseCase = false;
//Get the first selector from the array
string selector = selectors[0];
if (selectors.Length == 1)
{
baseCase = true;
}
else {
//If there are more selectors, then remove this selector and recurse with the rest
selectors = selectors.Where(w => w != selectors[0]).ToArray();
}
//If this is the first call...
if (element == null)
{
//Use the driver to select the element
selectorElement = Driver.FindElement(By.CssSelector(selector));
}
else {
//Otherwise, use the previously found element
selectorElement = element.FindElement(By.CssSelector(selector));
}
//Get the shadow root
root = (IWebElement)((IJavaScriptExecutor)Driver).ExecuteScript("return arguments[0].shadowRoot", selectorElement);
if (baseCase)
{
return root;
}
else {
//Recurse
root = FindShadowRootElementRecursive(selectors, root);
}
return root;
}
I then clicked the button like so:
PlayButton.Click();