2016-11-13 52 views
1

我正在創建maven應用程序,我從網站獲取信息。未找到maven包類selenium Web驅動程序

我創建Maven的包,但是當我使用命令運行的jar: 「Java的罐子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> 

請幫我找出什麼是錯的。謝謝!

回答

0

有可能是版本不匹配的機率如下:

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> 

保持硒的Java和HTMLUnitDriver的版本相同。

OR

有可能是對硒的服務器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

+0

感謝您的回覆。不幸的是它沒有幫助。 Htmlunit驅動程序最新版本是2.52.0。我將selenium-java的版本更改爲2.52.0,但沒有成功。 – Liam

+0

您是否嘗試添加硒服務器(用於版本2.52.0)的依賴關係? –

+0

是的,仍然是相同的情況:( – Liam