我正在创建从网站获取信息的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>
请帮助我找出问题所在。谢谢!
最佳答案
版本可能不匹配,如下所示:
从2.53.0开始,您需要显式包含HtmlUnitDriver作为要包含它的依赖项。驱动程序的版本号现在正在跟踪HtmlUnit本身。
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>htmlunit-driver</artifactId>
<version>2.53.1</version>
</dependency>
保留相同版本的Selenium-Java和HTMLUnitDriver。
要么
可能依赖于selenium-server-standalone.jar
如果使用的是DefaultSelenium(或RemoteWebDriver实现),则仍需要启动Selenium服务器。最好的方法是从“硒下载”页面下载selenium-server-standalone.jar并使用它。此外,如果将以下依赖项添加到pom.xml,则还可以将Selenium服务器嵌入到自己的项目中:
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-server</artifactId>
<version>3.0.1</version>
</dependency>
注意:请注意,如果您的项目将在Web应用程序容器中运行,则selenium-server工件与servlet-api-2.5工件具有依赖性,应将其排除在外。
参考:
http://www.seleniumhq.org/download/maven.jsp
关于java - 找不到Maven包类 Selenium Web驱动程序,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/40576010/