Web Driver click on element within ShadowDOM is returning error "{"message":"unknown error: Cannot read property 'defaultView' of undefined"

时间:2018-04-18 18:16:35

标签: java selenium webdriver polymer-2.x

I have a website built with Polymer 2 with a DOM structure that looks like:

...
<df-link slot="local-nav-drawer-link" link-href="/dictionary" text-color="light">
    #shadow-root (open)
    <a href="/dictionary" title="">
        <dom-if style="display: none;">
            <template is="dom-if"></template>
        </dom-if>
        <span class="dfpl-link__text">Dictionary</span>
    </a>
</df-link>
...

Using the page object pattern which defines the shadow root and the element I want to click as:

@FindBy(css = "df-link")
public WebElement shadowRootElement;

public By shadowAnchorElementSelector = By.cssSelector("a");

The code to retrieve the shadow root element looks like:

WebElement shadowRoot;
shadowRoot = (WebElement) jsExec.executeScript("return arguments[0].shadowRoot;", shadowRootElement);
WebElement anchor = shadowRoot.findElement(shadowAnchorElementSelector);
anchor.click();

I can successfully get attribute values, css values, and the inner text. However when I call a click this is what happens.

Apr 18, 2018 10:25:28 AM org.openqa.selenium.remote.server.WebDriverServlet handle
INFO: Found handler: org.openqa.selenium.remote.server.ServicedSession@136d9af5
Apr 18, 2018 10:25:28 AM org.openqa.selenium.remote.server.WebDriverServlet lambda$handle$0
INFO: Handler thread for session 2525b788dd3471e51e986fed682f04ea (chrome): 
Executing POST on /session/2525b788dd3471e51e986fed682f04ea/element/0.23700467091154298-6/click (handler: ServicedSession)
Apr 18, 2018 10:25:28 AM org.openqa.selenium.remote.server.Passthrough handle
INFO: To upstream: {"id":"0.23700467091154298-6"}
Apr 18, 2018 10:25:28 AM org.openqa.selenium.remote.server.Passthrough handle
INFO: To downstream: {"sessionId":"2525b788dd3471e51e986fed682f04ea","status":13,"value":{"message":"unknown error: Cannot read property 'defaultView' of undefined\n  (Session info: chrome=66.0.3359.117)\n  (Driver info: chromedriver=2.38.551601 (edb21f07fc70e9027c746edd3201443e011a61ed),platform=Windows NT 10.0.16299 x86_64)"}}

It looks as though the element is found fine, and I read from it, but as soon as I call WebElement.click() the error "Cannot read property 'defaultView' of undefined" is returned from ChromeDriver. I have not seen this posted around, and I am really stuck, wondering if someone has an answer for this.

This is with Chrome 66 and ChromeDriver 2.38.

2 个答案:

答案 0 :(得分:1)

我只是遇到了这个问题,并且能够使用firefox和geckodriver而不是chrome驱动程序来修复它。修复了所有问题。

答案 1 :(得分:-1)

public void forceClickElement(WebElement element){

    jsExec.executeScript("arguments[0].click();", element);
}