Select Page

I was trying to select a child element of RootShadow object, so I tried to select it by IJavaScriptExecutor first:

IWebElement otpApplicationElement = (IWebElement) ((IJavaScriptExecutor)WebDriver).ExecuteScript("return document.getElementById('meansContainer').firstChild.shadowRoot");

Then select the child element with Xpath like below:

IWebElement openBankingApp = otpApplicationElement.FindElement(By.XPath("//*[contains(text()='Open de Mobiel Bankieren App')]")); 

Resulted in exception error:

Failed to execute 'evaluate' on 'Document': The node provided is '#document-fragment', which is not a valid context node type.

Solution was to change css selector or find by tag name like:

IWebElement otpApplicationElement = (IWebElement) ((IJavaScriptExecutor)WebDriver).ExecuteScript("return document.getElementById('meansContainer').firstChild.shadowRoot.querySelector(\"h2\")");

Or choose CssSelector by Selenium

IWebElement openBankingApp = otpApplication.FindElement(By.CssSelector("h2"));