ShadowRoot 不是元素的情况

发布于 01-14 18:18 字数 1375 浏览 3 评论 0原文

我正在编写一个测试(在 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:

"

enter image description here

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

此岸叶落2025-01-21 18:18:00

私有WebElement get_shadow_root(字符串shadow_root_description,字符串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);

        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;
    }
}

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;
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文