java - 未找到 maven 包类 selenium Web 驱动程序

标签 java maven selenium-webdriver

我正在创建 Maven 应用程序,我可以从网站获取信息。

我创建了maven包,但是当我使用命令运行jar时: “java -jar app-1.0-SNAPSHOT.jar”我收到此错误:

“java.lang.ClassNotFoundException:org.openqa.selenium.WebDriver”

当我在 IntelliJ 中运行它时效果很好。

这是我的 pom:

<groupId>app</groupId>
<artifactId>app</artifactId>
<version>1.0-SNAPSHOT</version>

<dependencies>
<dependency>
    <groupId>org.seleniumhq.selenium</groupId>
    <artifactId>selenium-java</artifactId>
    <version>2.53.1</version>
</dependency>
<dependency>
    <groupId>org.seleniumhq.selenium</groupId>
    <artifactId>selenium-htmlunit-driver</artifactId>
    <version>2.52.0</version>
</dependency>
    <dependency>
        <groupId>com.itextpdf</groupId>
        <artifactId>itextpdf</artifactId>
        <version>5.0.6</version>
    </dependency>
</dependencies>

<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <configuration>
                <source>1.8</source>
                <target>1.8</target>
            </configuration>
        </plugin>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-jar-plugin</artifactId>
            <configuration>
                <archive>
                    <manifest>
                        <addClasspath>true</addClasspath>
                        <classpathPrefix>lib/</classpathPrefix>
                        <mainClass>sample.Main</mainClass>
                    </manifest>
                </archive>
            </configuration>
        </plugin>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-dependency-plugin</artifactId>
            <version>2.10</version>
            <executions>
                <execution>
                    <id>copy-dependencies</id>
                    <phase>package</phase>
                    <goals>
                        <goal>copy-dependencies</goal>
                    </goals>
                    <configuration>
                        <includeScope>runtime</includeScope>
                        <outputDirectory>${project.build.directory}/dependency-jars/</outputDirectory>
                    </configuration>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>

请帮我看看哪里出了问题。谢谢!

最佳答案

可能存在版本不匹配的情况,如下所示:

Starting with 2.53.0 you need to explicitly include HtmlUnitDriver as a dependency to include it. Version number of the driver is now tracking HtmlUnit itself.

 <dependency>
    <groupId>org.seleniumhq.selenium</groupId>
    <artifactId>htmlunit-driver</artifactId>
    <version>2.53.1</version>
</dependency>  

保留相同版本的 Selenium-Java 和 HTMLUnitDriver。

可能存在对 selenium-server-standalone.jar 的依赖

If you are using DefaultSelenium (or the RemoteWebDriver implementation), you still need to start a Selenium server. The best way is to download the selenium-server-standalone.jar from the Selenium Downloads page and just use it. Furthermore you can also embed the Selenium server into your own project, if you add the following dependency to your pom.xml:

<dependency>
    <groupId>org.seleniumhq.selenium</groupId>
    <artifactId>selenium-server</artifactId>
    <version>3.0.1</version>
</dependency>  

Note: Be aware, that the selenium-server artifact has a dependency to the servlet-api-2.5 artifact, which you should exclude, if your project will be run inside a web application container.

引用:

http://www.seleniumhq.org/download/maven.jsp

关于java - 未找到 maven 包类 selenium Web 驱动程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40576010/

相关文章:

java - 有没有办法在调用 setSelectedItem() 时阻止 Action 监听器触发?

java - Spring Security 和 JSF,找不到资源/j_spring_security_check.jsp

java - Android 中的 JSON 到 Java 对象不适用于 GSON

java - 如何从 Jenkinsfile 执行 Maven 发布?

java - 在 'project' 和 'external' 依赖项之间切换的正确 Gradle 项目结构是什么

c# - Selenium 测试在 IE11 中不起作用

java - 使用 SSPI : no errors, 进行 Kerberos 模拟但不起作用

java - 如何使 m2e-android 包含 apk 打包的 native deps?

java - 获取选择字段的当前值

java - WebElement 的 setAttribute() 方法