Selenium 3.3.1:NoDefClassFound(com.google.common.base.Function)

时间:2017-03-25 22:55:04

标签: java maven selenium

我在使用最简单的代码尝试启动ChromeDriver(或FirefoxDriver)时遇到了Selenium 3.3.1最奇怪的问题:

修改:添加完整的课程

import java.util.concurrent.TimeUnit;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;

public class App {
    public static void main(String[] args) {
        // testCrawl();
        seleniumGetDoc();
    }

    public static void seleniumGetDoc() {
        System.setProperty("webdriver.chrome.driver", "path/to/chromedriver");
        WebDriver driver = new ChromeDriver(); //here something goes terribly wrong
        driver.manage().timeouts().implicitlyWait(15, TimeUnit.SECONDS);
        driver.get("https://google.com/");
        String htmlContent = driver.getPageSource();
        System.out.println(driver.getCurrentUrl());
    }
}

我的pom中有以下依赖项:

    <dependency>
        <groupId>org.seleniumhq.selenium</groupId>
        <artifactId>selenium-java</artifactId>
        <version>3.3.1</version>
    </dependency>
    <!-- https://mvnrepository.com/artifact/org.seleniumhq.selenium/selenium-server -->
    <dependency>
        <groupId>org.seleniumhq.selenium</groupId>
        <artifactId>selenium-server</artifactId>
        <version>3.3.1</version>
    </dependency>

我获得以下输出:

Exception in thread "main" java.lang.NoClassDefFoundError: com/google/common/base/Function
    at my.package.MyClass.seleniumGetDoc(CrawlerMain.java:28)
    at  my.package.MyClass.main(CrawlerMain.java:22)
Caused by: java.lang.ClassNotFoundException: com.google.common.base.Function
    at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:331)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:357)

尽管如此,在降级我的selenium版本时,它可以正常使用以下依赖项:

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

有谁知道为什么显然在3.3.1版本中缺少一些番石榴图书馆?或者我在设置Selenium时遗漏了什么?

我已经查看了StackOverflow上的几篇帖子,但是这个问题要么与缺少的外部jar安装(maven应该处理)有关,要么与selenium-server缺少相关性。

编辑我的依赖项列表:

<dependencies>
    <!-- https://mvnrepository.com/artifact/org.jsoup/jsoup -->
    <dependency>
        <groupId>org.jsoup</groupId>
        <artifactId>jsoup</artifactId>
        <version>1.10.2</version>
    </dependency>

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

    <dependency>
        <groupId>org.apache.logging.log4j</groupId>
        <artifactId>log4j-api</artifactId>
        <version>2.8</version>
    </dependency>
    <dependency>
        <groupId>org.apache.logging.log4j</groupId>
        <artifactId>log4j-core</artifactId>
        <version>2.8</version>
    </dependency>
    <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <version>4.12</version>
        <scope>test</scope>
    </dependency>
</dependencies>

同样在运行mvn dependency:tree时,maven表示只有 selenium-java 取决于com.google.guava:guava:jar:21.0:compile

编辑添加我的依赖关系树:

[INFO] org.linkedin.crawler:main:jar:0.0.1-SNAPSHOT
[INFO] +- org.jsoup:jsoup:jar:1.10.2:compile
[INFO] +- org.seleniumhq.selenium:selenium-java:jar:3.3.1:compile
[INFO] |  +- org.seleniumhq.selenium:selenium-chrome-driver:jar:3.3.1:compile
[INFO] |  |  \- org.seleniumhq.selenium:selenium-remote-driver:jar:3.3.1:compile
[INFO] |  |     +- org.seleniumhq.selenium:selenium-api:jar:3.3.1:compile
[INFO] |  |     +- cglib:cglib-nodep:jar:3.2.4:compile
[INFO] |  |     +- org.apache.commons:commons-exec:jar:1.3:compile
[INFO] |  |     +- com.google.code.gson:gson:jar:2.8.0:compile
[INFO] |  |     +- com.google.guava:guava:jar:21.0:compile
[INFO] |  |     +- org.apache.httpcomponents:httpclient:jar:4.5.2:compile
[INFO] |  |     |  \- org.apache.httpcomponents:httpcore:jar:4.4.4:compile
[INFO] |  |     \- net.java.dev.jna:jna-platform:jar:4.1.0:compile
[INFO] |  |        \- net.java.dev.jna:jna:jar:4.1.0:compile
[INFO] |  +- org.seleniumhq.selenium:selenium-edge-driver:jar:3.3.1:compile
[INFO] |  +- org.seleniumhq.selenium:selenium-firefox-driver:jar:3.3.1:compile
[INFO] |  +- org.seleniumhq.selenium:selenium-ie-driver:jar:3.3.1:compile
[INFO] |  +- org.seleniumhq.selenium:selenium-opera-driver:jar:3.3.1:compile
[INFO] |  +- org.seleniumhq.selenium:selenium-safari-driver:jar:3.3.1:compile
[INFO] |  +- com.codeborne:phantomjsdriver:jar:1.4.0:compile
[INFO] |  \- org.seleniumhq.selenium:htmlunit-driver:jar:2.24:compile
[INFO] |     +- org.seleniumhq.selenium:selenium-support:jar:3.3.1:compile (version selected from constraint [2.53.0,4.0.0))
[INFO] |     |  \- org.hamcrest:hamcrest-library:jar:1.3:compile
[INFO] |     \- net.sourceforge.htmlunit:htmlunit:jar:2.24:compile
[INFO] |        +- xalan:xalan:jar:2.7.2:compile
[INFO] |        |  \- xalan:serializer:jar:2.7.2:compile
[INFO] |        +- org.apache.commons:commons-lang3:jar:3.5:compile
[INFO] |        +- org.apache.httpcomponents:httpmime:jar:4.5.2:compile
[INFO] |        +- commons-codec:commons-codec:jar:1.10:compile
[INFO] |        +- net.sourceforge.htmlunit:htmlunit-core-js:jar:2.23:compile
[INFO] |        +- net.sourceforge.htmlunit:neko-htmlunit:jar:2.24:compile
[INFO] |        |  \- xerces:xercesImpl:jar:2.11.0:compile
[INFO] |        |     \- xml-apis:xml-apis:jar:1.4.01:compile
[INFO] |        +- net.sourceforge.cssparser:cssparser:jar:0.9.21:compile
[INFO] |        |  \- org.w3c.css:sac:jar:1.3:compile
[INFO] |        +- commons-io:commons-io:jar:2.5:compile
[INFO] |        +- commons-logging:commons-logging:jar:1.2:compile
[INFO] |        \- org.eclipse.jetty.websocket:websocket-client:jar:9.2.20.v20161216:compile
[INFO] |           +- org.eclipse.jetty:jetty-util:jar:9.2.20.v20161216:compile
[INFO] |           +- org.eclipse.jetty:jetty-io:jar:9.2.20.v20161216:compile
[INFO] |           \- org.eclipse.jetty.websocket:websocket-common:jar:9.2.20.v20161216:compile
[INFO] |              \- org.eclipse.jetty.websocket:websocket-api:jar:9.2.20.v20161216:compile
[INFO] +- org.seleniumhq.selenium:selenium-server:jar:3.3.1:compile
[INFO] |  +- com.beust:jcommander:jar:1.48:compile
[INFO] |  +- net.jcip:jcip-annotations:jar:1.0:compile
[INFO] |  +- org.seleniumhq.selenium:jetty-repacked:jar:9.4.1.v20170120:compile
[INFO] |  |  \- javax.servlet:javax.servlet-api:jar:3.1.0:compile
[INFO] |  \- org.yaml:snakeyaml:jar:1.15:compile
[INFO] +- org.apache.logging.log4j:log4j-api:jar:2.8:compile
[INFO] +- org.apache.logging.log4j:log4j-core:jar:2.8:compile
[INFO] \- junit:junit:jar:4.12:test
[INFO]    \- org.hamcrest:hamcrest-core:jar:1.3:compile

更新:它适用于selenium-server版本3.3.1和selenium-java版本3.0.1,从selenium-java版本&gt; 3.1.x启动打破上述相同的错误

修改添加完整的pom:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>org.main.groupid</groupId>
    <artifactId>main</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>

    <name>main</name>
    <url>http://maven.apache.org</url>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <maven.compiler.source>1.8</maven.compiler.source>
        <maven.compiler.target>1.8</maven.compiler.target>
    </properties>

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-dependency-plugin</artifactId>
                <version>3.0.0</version>
                <executions>
                    <execution>
                        <id>copy-dependencies</id>
                        <phase>package</phase>
                        <goals>
                            <goal>copy-dependencies</goal>
                        </goals>
                        <configuration>
                            <outputDirectory>${project.build.directory}/lib/</outputDirectory>
                            <overWriteReleases>false</overWriteReleases>
                            <overWriteSnapshots>false</overWriteSnapshots>
                            <overWriteIfNewer>true</overWriteIfNewer>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
            <plugin>
                <!-- Build an executable JAR -->
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-jar-plugin</artifactId>
                <version>3.0.2</version>
                <configuration>
                    <archive>
                        <manifest>
                            <addClasspath>true</addClasspath>
                            <classpathPrefix>lib/</classpathPrefix>
                            <mainClass>my.main.Class</mainClass>
                        </manifest>
                    </archive>
                    <excludes>
                        <exclude>**/*log4j*</exclude>
                    </excludes>
                </configuration>
            </plugin>


        </plugins>
    </build>

    <dependencies>
        <dependency>
            <groupId>org.jsoup</groupId>
            <artifactId>jsoup</artifactId>
            <version>1.10.2</version>
        </dependency>
        <dependency>
            <groupId>org.seleniumhq.selenium</groupId>
            <artifactId>selenium-java</artifactId>
            <version>3.3.1</version>
        </dependency>
        <dependency>
            <groupId>org.seleniumhq.selenium</groupId>
            <artifactId>selenium-server</artifactId>
            <version>3.3.1</version>
        </dependency>



        <dependency>
            <groupId>org.apache.logging.log4j</groupId>
            <artifactId>log4j-api</artifactId>
            <version>2.8</version>
        </dependency>
        <dependency>
            <groupId>org.apache.logging.log4j</groupId>
            <artifactId>log4j-core</artifactId>
            <version>2.8</version>
        </dependency>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
            <scope>test</scope>
        </dependency>
    </dependencies>
</project>

3 个答案:

答案 0 :(得分:1)

有趣。留下这个作为答案,因为评论太长了。我试图重现你的问题,它对我来说很好。你有没有其他的东西添加到你的课程路径?您是在命令行中从IDE还是从maven命令运行?你还使用的是什么版本的chrome,你的chrome版本是否有正确的chromedriver版本?

https://sites.google.com/a/chromium.org/chromedriver/downloads

我使用了这个测试代码:

package com.foo;

import org.junit.Test;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import java.util.concurrent.TimeUnit;

public class SeleniumTest {

    @Test
    public void testFoo() throws Exception {
        System.setProperty("webdriver.chrome.driver", "MY_CHROMEDRIVER_PATH");
        WebDriver driver = new ChromeDriver();
        driver.manage().timeouts().implicitlyWait(15, TimeUnit.SECONDS);
        driver.get("https://google.com/");
        String htmlContent = driver.getPageSource();
        System.out.println(driver.getCurrentUrl());
        System.out.println(htmlContent);
        Thread.sleep(2_000);
        driver.quit();
    }
}

这个pom文件:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.foo</groupId>
    <artifactId>selenium-test</artifactId>
    <version>1.0-SNAPSHOT</version>

    <dependencies>
        <dependency>
            <groupId>org.jsoup</groupId>
            <artifactId>jsoup</artifactId>
            <version>1.10.2</version>
        </dependency>
        <dependency>
            <groupId>org.seleniumhq.selenium</groupId>
            <artifactId>selenium-java</artifactId>
            <version>3.3.1</version>
        </dependency>
        <dependency>
            <groupId>org.seleniumhq.selenium</groupId>
            <artifactId>selenium-server</artifactId>
            <version>3.3.1</version>
        </dependency>
        <dependency>
            <groupId>org.apache.logging.log4j</groupId>
            <artifactId>log4j-api</artifactId>
            <version>2.8</version>
        </dependency>
        <dependency>
            <groupId>org.apache.logging.log4j</groupId>
            <artifactId>log4j-core</artifactId>
            <version>2.8</version>
        </dependency>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
            <scope>test</scope>
        </dependency>
    </dependencies>
</project>

答案 1 :(得分:1)

我遇到了类似的问题,并将其缩小到包括Selenium 3.3.1 Standalone和HTMLUnit-driver-standalone 2.21。结果我根本不需要HTMLUnit jar。它已经包含在3.3.1依赖项中。 (如果我没记错的话,最后一个版本没有包含HTMLUnit,这就是为什么我之前单独添加它...这可以解释&gt; 3.01部分。)

现在,我没有使用Selenium-java部分...只是独立服务器,但看起来你的dep列表中有2个HTMLUnit版本... 2.24和2.23,所以它可能是一个类似的问题。

答案 2 :(得分:0)

我的classpath中有2个不同版本的guava.jar导致了这个问题,从我的类路径中移除旧版本就可以了。