使用Selenium Chrome WebDriver的NoClassDefFoundError

时间:2016-07-13 09:33:42

标签: java selenium selenium-webdriver webdriver

这是我的班级。我使用Selenium Chrome WebDriver在网站上自动执行操作。我的问题是我得到了一个N​​oClassDefFoundError,但这是不可能的,因为我已经在我的构建路径中包含了所有必需的.jars文件。发生了什么事?

public class TwitterAutoBot {
    private WebDriver driver;
    private String baseUrl;
    private StringBuffer verificationErrors = new StringBuffer();

    @Before
    public void setUp() throws Exception {

        // Google Chrome
        File file = new File("C:\\Users\\User\\plugins\\TwitterAutoBot\\src\\chromedriver.exe");
        System.setProperty("webdriver.chrome.driver", file.getAbsolutePath());
        driver = new ChromeDriver();

        // Firefox
        // driver = new FirefoxDriver();

        baseUrl = "https://www.twitter.com";
        driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
    }

    @Test
    public void testValidation() throws Exception {

        // Authentication information
        String username = "asdfasdfasdf";
        String password = "asdf123!";

        // Open site
        launchActivity("/");

        // Maximize window
        maximizeWindow();

        // Enter authentication information
        sendKeysAndWait("signin-email", username);
        sendKeysAndWait("signin-password", password);

        // Log in
        clickButtonAndWait("//button[@type='submit']");

        // Send 40 messages per hour for a total of 960 per day
        for (int i = 1; i < 960; i++) {
            sendMessageAndWait();
        }

        System.out.println("All messages have been sent.");

    }

      public void sendMessageAndWait() throws Exception {
        driver.findElement(By.id("tweet-box-home-timeline")).click();
        driver.findElement(By.xpath("(//button[@type='button'])[17]")).click();
        driver.findElement(By.id("global-new-tweet-button")).click();
        driver.findElement(By.xpath("(//button[@type='button'])[644]")).click();
        System.out.println("Message sent.");
        sleep();
      }

    private void maximizeWindow() throws InterruptedException {
        driver.manage().window().maximize();
        sleep();
    }

    /**
     * 
     * @param extension The remainder of the URL, after baseUrl
     */
    private void launchActivity(String extension) {
        driver.get(baseUrl + extension);
    }

    /**s
     * Writes a string to an HTML element and waits a random time
     * @param elem The element to send the keys
     * @param keys The string to write to the element
     * @throws InterruptedException
     */
    private void sendKeysAndWait(String elem, String keys) throws InterruptedException {
        Math.random();
        driver.findElement(By.id(elem)).clear();
        driver.findElement(By.id(elem)).sendKeys(keys);
        sleep();
    }

    /**
     * 
     * @param id The id of the button found in HTML
     * @throws InterruptedException
     */
    private void clickButtonAndWait(String id) throws InterruptedException {
        driver.findElement(By.id(id)).click();
        sleep();
    }


    private void sleep() throws InterruptedException {
        int randomWaitDuration = 75000 + (int)(Math.random() * 105000); ;
        Thread.sleep(randomWaitDuration);
        System.out.println(randomWaitDuration + " minutes since your last tweet.");
    }

    @After
    public void tearDown() throws Exception {
        driver.quit();
        String verificationErrorString = verificationErrors.toString();
        if (!"".equals(verificationErrorString)) {
            fail(verificationErrorString);
        }
    }
}

我得到以下 NoClassDefFoundError:

java.lang.NoClassDefFoundError: org/openqa/selenium/remote/RenderedRemoteWebElement
    at java.lang.ClassLoader.defineClass1(Native Method)
    at java.lang.ClassLoader.defineClass(ClassLoader.java:763)
    at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:142)
    at java.net.URLClassLoader.defineClass(URLClassLoader.java:467)
    at java.net.URLClassLoader.access$100(URLClassLoader.java:73)
    at java.net.URLClassLoader$1.run(URLClassLoader.java:368)
    at java.net.URLClassLoader$1.run(URLClassLoader.java:362)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(URLClassLoader.java:361)
    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)
    at TwitterAutoBot.setUp(TwitterAutoBot.java:33)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:45)
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)
    at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:42)
    at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:27)
    at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:30)
    at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:263)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:68)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:47)
    at org.junit.runners.ParentRunner$3.run(ParentRunner.java:231)
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:60)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:229)
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:50)
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:222)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:300)
    at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:86)
    at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:459)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:675)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:382)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:192)
Caused by: java.lang.ClassNotFoundException: org.openqa.selenium.remote.RenderedRemoteWebElement
    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)
    ... 37 more

java.lang.NullPointerException
    at TwitterAutoBot.tearDown(TwitterAutoBot.java:126)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:45)
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)
    at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:42)
    at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:36)
    at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:263)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:68)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:47)
    at org.junit.runners.ParentRunner$3.run(ParentRunner.java:231)
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:60)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:229)
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:50)
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:222)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:300)
    at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:86)
    at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:459)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:675)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:382)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:192)

Java模块:

junit-4.10.jar
selenium-chrome-driver-2.0a4.jar
selenium-java-2.53.0-srcs.jar
selenium-java-2.53.0.jar
selenium-server-standalone-2.53.0.jar

1 个答案:

答案 0 :(得分:1)

似乎是一个jar无法解决的问题。请下载jar“selenium-remote-client-2.0a5.jar”文件并将其添加到类路径中。

Jar可以在

中找到

Jar file