WebDriverManager 抛出 TypeNotPresentException

WebDriverManager throwing TypeNotPresentException

我正在尝试用 WebDriverManager 替换我的本地 geckodriver.exe,但是当我调用

WebDriverManager.firefoxdriver().setup();

我得到以下错误

4391 [main] INFO io.github.bonigarcia.wdm.WebDriverManager - Reading https://api.github.com/repos/mozilla/geckodriver/releases to seek geckodriver
4704 [main] WARN io.github.bonigarcia.wdm.WebDriverManager - There was an error managing geckodriver (latest version) (Type com.google.gson.internal.LinkedTreeMap not present) ... trying again using latest driver stored in cache
4704 [main] INFO io.github.bonigarcia.wdm.WebDriverManager - Reading https://api.github.com/repos/mozilla/geckodriver/releases to seek geckodriver
4876 [main] ERROR io.github.bonigarcia.wdm.WebDriverManager - There was an error managing geckodriver (latest version) (Type com.google.gson.internal.LinkedTreeMap not present)
java.lang.TypeNotPresentException: Type com.google.gson.internal.LinkedTreeMap not present
    at sun.reflect.generics.factory.CoreReflectionFactory.makeNamedType(CoreReflectionFactory.java:117)
    at sun.reflect.generics.visitor.Reifier.visitClassTypeSignature(Reifier.java:125)
    at sun.reflect.generics.tree.ClassTypeSignature.accept(ClassTypeSignature.java:49)
    at sun.reflect.generics.visitor.Reifier.reifyTypeArguments(Reifier.java:68)
    ...
    at io.github.bonigarcia.wdm.WebDriverManager.fallback(WebDriverManager.java:825)
    at io.github.bonigarcia.wdm.WebDriverManager.handleException(WebDriverManager.java:802)
    at io.github.bonigarcia.wdm.WebDriverManager.manage(WebDriverManager.java:599)
    at io.github.bonigarcia.wdm.WebDriverManager.setup(WebDriverManager.java:287)

我正在使用来自 maven 的最新 5.0.3 WebDriverManager

<dependency>
    <groupId>io.github.bonigarcia</groupId>
    <artifactId>webdrivermanager</artifactId>
    <version>5.0.3</version>
</dependency>

pom.xml中将WebDriverManager范围更改为test 如下:

<dependency>
    <groupId>io.github.bonigarcia</groupId>
    <artifactId>webdrivermanager</artifactId>
    <version>5.0.3</version>
    <scope>test</scope>
</dependency>

或在pom.xml[=42=中将范围更改为compile ]如下:

<dependency>
    <groupId>io.github.bonigarcia</groupId>
    <artifactId>webdrivermanager</artifactId>
    <version>5.0.3</version>
    <scope>compile</scope>
</dependency>

此外,您可能需要添加 slf4j 依赖,如下所示:

<dependency>
    <groupId>org.slf4j</groupId>
    <artifactId>slf4j-api</artifactId>
    <version>1.7.25</version>
</dependency>

根据Driver Management - Feature

Although not mandatory, it is highly recommended to use a logger library to trace your application and tests. In the case of WebDriverManager, you will see the relevant steps of the driver management following its traces. See for example the following tutorial to use SLF4J and Logback. Also, you can see an example of a WebDriverManager test using logging here (this example uses this configuration file).


参考

您可以在以下位置找到一些相关的详细讨论:

TypeNotPresentException 当应用程序尝试使用表示类型名称的字符串访问类型,但找不到具有指定名称的类型的定义时抛出。

它与 ClassNotFoundException 非常相似,但在编译期间未检查。

但根本原因是:

com.google.gson.internal.LinkedTreeMap 不存在于项目类路径中。

尝试为项目添加gson依赖。

<dependency>
    <groupId>com.google.code.gson</groupId>
    <artifactId>gson</artifactId>
    <version>2.9.0</version>
</dependency>