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);
WebElement shadow_root = null;
Object o = ((JavascriptExecutor) driver).executeScript("return arguments[0].shadowRoot", root_element);
if (o instanceof WebElement) {
shadow_root = (WebElement) o;
} else {
// https://titusfortner.com/2021/11/22/shadow-dom-selenium.html
Map<?, ?> m = (Map<?, ?>) o;
String id = (String) m.entrySet().iterator().next().getValue();
shadow_root = new RemoteWebElement();
((RemoteWebElement) shadow_root).setParent((RemoteWebDriver) driver);
((RemoteWebElement) shadow_root).setId(id);
}
return shadow_root;
} catch (ClassCastException e) {
WebElement root_element = driver.findElement(By.xpath(shadow_root_xpath));
JavascriptExecutor jse = (JavascriptExecutor) driver;
SearchContext shadowRoot = (SearchContext) jse.executeScript("return arguments[0].shadowRoot",
root_element);
WebElement shadowContent = shadowRoot.findElement(By.cssSelector("div"));
return shadowContent;
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.