ShadowRoot 不是元素的情况
我正在编写一个测试(在 Java 中使用 Selenium),其中 HTML 包含一个影子根,如下图所示(影子根下有许多元素):
有一些代码可供其他项目获取根:
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;
}
所以这个由以下代码调用
get_local_shadow_root("A description ignored", "//research-provider-comp/span" );
waitForXpathVisibility() 方法只是在 xpath 上查找并返回它。 不管怎样,这对其他人有用,但对我来说,我收到一条消息,org.openqa.selenium.remote.ShadowRoot 无法转换为 org.openqa.selenium.WebElement 。
System.out.println(o.getClass())
返回一个类 org.openqa.selenium.remote.ShadowRoot。我无法创建这种类型的变量,因为 org.openqa.selenium.remote.ShadowRoot 不可见。
那么有什么建议吗?作为示例,以下是 ShadowRoot 中的字段(邮政编码)示例:
<input type="text" name="zipCode" data-testid="zip-code-text-box" class="width100" value="">
HTML 中带有 #shadow-root,如下:
"
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 thatorg.openqa.selenium.remote.ShadowRoot cannot be cast toorg.openqa.selenium.WebElement.
System.out.println(o.getClass())
returns aclass org.openqa.selenium.remote.ShadowRoot. I cannot make a variable of this type asorg.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:
"
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

私有WebElement get_shadow_root(字符串shadow_root_description,字符串shadow_root_xpath){
尝试 {
WebElement root_element = waitForXPathVisibility(shadow_root_description, Shadow_root_xpath);
private WebElement get_shadow_root(String shadow_root_description, String shadow_root_xpath) {
try {
WebElement root_element = waitForXPathVisibility(shadow_root_description, shadow_root_xpath);