java.lang.ClassNotFoundException:WebDriver API

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3499847/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-14 01:20:01  来源:igfitidea点击:

java.lang.ClassNotFoundException: WebDriver API

javaseleniumwebdriver

提问by KJW

I downloaded the selenium-java-2.0a5.zip

我下载了 selenium-java-2.0a5.zip

http://code.google.com/p/selenium/downloads/list

http://code.google.com/p/selenium/downloads/list

and ran the following code:

并运行以下代码:

package org.openqa.selenium.example;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.htmlunit.HtmlUnitDriver;

public class Example  {
    public static void main(String[] args) {
        // Create a new instance of the html unit driver
        // Notice that the remainder of the code relies on the interface, 
        // not the implementation.
        WebDriver driver = new HtmlUnitDriver();

        // And now use this to visit Google
        driver.get("http://www.google.com");

        // Find the text input element by its name
        WebElement element = driver.findElement(By.name("q"));

        // Enter something to search for
        element.sendKeys("Cheese!");

        // Now submit the form. WebDriver will find the form for us from the element
        element.submit();

        // Check the title of the page
        System.out.println("Page title is: " + driver.getTitle());
    }
}

but then I got

但后来我得到了

    at org.openqa.selenium.example.Example.main(Example.java:13)
Caused by: java.lang.ClassNotFoundException: com.gargoylesoftware.htmlunit.WebWindowListener
    at java.net.URLClassLoader.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(Unknown Source)
    at java.lang.ClassLoader.loadClass(Unknown Source)
    at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
    at java.lang.ClassLoader.loadClass(Unknown Source)
    ... 1 more

Did I miss a step ? I made sure to import the selenium-java-2.0a5.jarinto the project.

我错过了一步吗?我确保将selenium-java-2.0a5.jar导入到项目中。

采纳答案by Harsha Hulageri

htmlunit jar is not in the classpath. Include dependency lib jars of selenium-java-2.0a5.jar as well. I am sure they must have been provided in the zip you downloaded

htmlunit jar 不在类路径中。还包括 selenium-java-2.0a5.jar 的依赖库 jar。我确定它们必须已在您下载的 zip 中提供

回答by YoK

You seem to use "HtmlUnit"in your project and its jar is missing from classpath. Add it to your project properties as you added selenium.

您似乎在项目中使用了“HtmlUnit”,但类路径中缺少它的 jar。添加硒时将其添加到您的项目属性中。

回答by Dave Hunt

You want to download selenium-server-standalone-2.0a5.jarfrom http://code.google.com/p/selenium/downloads/listinstead, as this includes dependencies.

您想selenium-server-standalone-2.0a5.jarhttp://code.google.com/p/selenium/downloads/list下载,因为这包括依赖项。