I am writing a test (in Java with Selenium) where the HTML contains a shadow root as in the picture below (there are many elements are under the shadow root):
There is some code existing for other projects to get the root:
private WebElement get_local_shadow_root(String shadow_root_description, String shadow_root_xpath) {
WebElement root_element = waitForXPathVisibility(shadow_root_description, shadow_root_xpath);
WebElement shadow_root = null;
Object o = ((JavascriptExecutor) driver).executeScript("return arguments[0].shadowRoot", root_element);
shadow_root = (WebElement) o;
return (WebElement) shadow_root;
}
So this is called by the following
get_local_shadow_root("A description ignored", "//research-provider-comp/span" );
The waitForXpathVisibility() method just does a find on the xpath and returns it. Anyway, this is working for others, but for me I get a message that org.openqa.selenium.remote.ShadowRoot cannot be cast to org.openqa.selenium.WebElement.
System.out.println(o.getClass())
returns a class org.openqa.selenium.remote.ShadowRoot. I cannot make a variable of this type as org.openqa.selenium.remote.ShadowRoot is not visible.
So any suggestions what to do? Just as an example, here is an example of a field (zip code) in the ShadowRoot:
<input type="text" name="zipCode" data-testid="zip-code-text-box" class="width100" value="">
HTML with #shadow-root below:
"
private WebElement get_shadow_root(String shadow_root_description, String shadow_root_xpath) { try { WebElement root_element = waitForXPathVisibility(shadow_root_description, shadow_root_xpath);