java.base/java.lang.reflect.Constructor.newInstance(Constructor.java:481) 在 Selenium 和 Java 中使用不正确的代码时

java.base/java.lang.reflect.Constructor.newInstance(Constructor.java:481) when using improper code with Selenium and Java

为了快速制作原型,我使用了下面的 Selenium Webdriver java 代码(事实上,它不符合最佳实践,但无论如何它应该可以工作,尽管不是最佳的) .

if (!driver.findElements(By.xpath("//div[@class='ui-lib-popup-element__close']")).isEmpty()) {
    driver.findElement(By.xpath("//div[@class='ui-lib-popup-element__close']")).click();
}

即它在 findElement() 中具有相同的值。当它被放入 while 循环时,它会在某个循环中抛出异常。

Capabilities {acceptInsecureCerts: true, browserName: firefox, browserVersion: 77.0.1, javascriptEnabled: true, moz:accessibilityChecks: false, moz:buildID: 20200602222727, moz:geckodriverVersion: 0.26.0, moz:headless: false, moz:processID: 12120, moz:profile: C:\Users\eljah32\AppData\Lo..., moz:shutdownTimeout: 60000, moz:useNonSpecCompliantPointerOrigin: false, moz:webdriverClick: true, pageLoadStrategy: normal, platform: WINDOWS, platformName: WINDOWS, platformVersion: 10.0, rotatable: false, setWindowRect: true, strictFileInteractability: false, timeouts: {implicit: 0, pageLoad: 300000, script: 30000}, unhandledPromptBehavior: dismiss and notify}
Session ID: 77ad55a3-7cc3-4126-a91d-f0b63c438520
    at java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
    at java.base/jdk.internal.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
    at java.base/java.lang.reflect.Constructor.newInstanceWithCaller(Constructor.java:500)
    at java.base/java.lang.reflect.Constructor.newInstance(Constructor.java:481)
    at org.openqa.selenium.remote.http.W3CHttpResponseCodec.createException(W3CHttpResponseCodec.java:187)
    at org.openqa.selenium.remote.http.W3CHttpResponseCodec.decode(W3CHttpResponseCodec.java:122)
    at org.openqa.selenium.remote.http.W3CHttpResponseCodec.decode(W3CHttpResponseCodec.java:49)
    at org.openqa.selenium.remote.HttpCommandExecutor.execute(HttpCommandExecutor.java:158)
    at org.openqa.selenium.remote.service.DriverCommandExecutor.execute(DriverCommandExecutor.java:83)
    at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:552)
    at org.openqa.selenium.remote.RemoteWebElement.execute(RemoteWebElement.java:285)
    at org.openqa.selenium.remote.RemoteWebElement.click(RemoteWebElement.java:84)
    at SeleniumConfirmRNN.main(SeleniumConfirmRNN.java:122)

Selenium 库版本:

<dependency>
            <groupId>org.seleniumhq.selenium</groupId>
            <artifactId>selenium-java</artifactId>
            <version>3.141.59</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.seleniumhq.selenium/selenium-firefox-driver -->
        <dependency>
            <groupId>org.seleniumhq.selenium</groupId>
            <artifactId>selenium-firefox-driver</artifactId>
            <version>3.141.59</version>
        </dependency>
        <dependency>
            <groupId>org.seleniumhq.selenium</groupId>
            <artifactId>selenium-support</artifactId>
            <version>3.0.1</version>
        </dependency>

那么,如何处理这个问题,它是一个错误吗? FirefoxDriver.

会发生这种情况

这个错误信息...

Capabilities {acceptInsecureCerts: true, browserName: firefox, browserVersion: 77.0.1, javascriptEnabled: true, moz:accessibilityChecks: false, moz:buildID: 20200602222727, moz:geckodriverVersion: 0.26.0, moz:headless: false, moz:processID: 12120, moz:profile: C:\Users\eljah32\AppData\Lo..., moz:shutdownTimeout: 60000, moz:useNonSpecCompliantPointerOrigin: false, moz:webdriverClick: true, pageLoadStrategy: normal, platform: WINDOWS, platformName: WINDOWS, platformVersion: 10.0, rotatable: false, setWindowRect: true, strictFileInteractability: false, timeouts: {implicit: 0, pageLoad: 300000, script: 30000}, unhandledPromptBehavior: dismiss and notify}
Session ID: 77ad55a3-7cc3-4126-a91d-f0b63c438520
    at java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)

...暗示 GeckoDriver 无法 click() 浏览上下文 [=48= 中的 ] 即 Firefox 浏览器 会话。


您需要考虑以下几件事:

  • 首先,您不会在 <div> 元素上调用 click(),除非该元素包含 contenteditable="true" 属性。

You can find a detailed discussion in

  • 据推测,应该有一个子 <input><span> 元素,这是您所指的 <div> 中您想要的元素。因此,您需要更深入地定位 element inducing for the elementToBeClickable() and you can use the following :

    try {
      new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("//div[@class='ui-lib-popup-element__close']//input"))).click();
      System.out.println("Element was clicked");
    }
    catch(TimeoutException e) {
      System.out.println("Element wasn't clicked");
    }
    

参考

您可以在以下位置找到相关讨论:

  • sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) using findElement(By.className()) through Selenium and Java