首页 > 解决方案 > 硒:NoClassDefFoundError WebDriverException

问题描述

在某些(看似随机的)情况下,使用 Selenium 和 Chrome 无头网络驱动程序时出现以下错误:

java.lang.NoClassDefFoundError: Could not initialize class org.openqa.selenium.WebDriverException

我猜某些元素在导致异常的浏览器中不可用,但没有异常消息我无法确定。

我的 pom.xml 中有这个:

<dependency>
            <groupId>org.seleniumhq.selenium</groupId>
            <artifactId>selenium-java</artifactId>
            <version>4.0.0-alpha-7</version>
</dependency>

我可以在我的代码中创建一个 WebDriverException 的虚拟实例。那么可能是什么问题?

更新:我还看到了其他类型的 Selenium 异常,没有消息。这是堆栈跟踪的另一个示例:

java.lang.NoClassDefFoundError: Could not initialize class org.openqa.selenium.ElementClickInterceptedException
    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.newInstance(Constructor.java:490)
    at org.openqa.selenium.remote.codec.w3c.W3CHttpResponseCodec.createException(W3CHttpResponseCodec.java:196)
    at org.openqa.selenium.remote.codec.w3c.W3CHttpResponseCodec.decode(W3CHttpResponseCodec.java:129)
    at org.openqa.selenium.remote.codec.w3c.W3CHttpResponseCodec.decode(W3CHttpResponseCodec.java:53)
    at org.openqa.selenium.remote.HttpCommandExecutor.execute(HttpCommandExecutor.java:167)
    at org.openqa.selenium.remote.service.DriverCommandExecutor.execute(DriverCommandExecutor.java:123)
    at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:501)
    at org.openqa.selenium.remote.RemoteWebElement.execute(RemoteWebElement.java:238)
    at org.openqa.selenium.remote.RemoteWebElement.click(RemoteWebElement.java:79)

标签: javaseleniumselenium-webdriver

解决方案


WebDriverException类是Selenium API发行版的一部分。您的 POM 中缺少该依赖项。除此之外,除非您正在进行评估,否则请远离 alpha 版本。始终使用稳定版本。


推荐阅读