简体   繁体   中英

run test with selenium web driver - NoClassDefFoundError

I tried to figure out how works - selenium web driver.

And tried to run easy program, but caught next error at console:

java.lang.NoClassDefFoundError: selenium/web/driver/Selenium2Example
Caused by: java.lang.ClassNotFoundException: selenium.web.driver.Selenium2Example
    at java.net.URLClassLoader$1.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)
    at java.lang.ClassLoader.loadClassInternal(Unknown Source)
Exception in thread "main" 

and pop-up window with message:

Could not find the main class, program will exit

I'm using Eclipse Helios under Windows XP.
I attached neded -jars for progect ( selenium-java-2.35.0 + selenium-server-standalone-2.35.0 ).

Here is code:

public class Selenium2Example {

    /**
     * @param args
     */
    public static void main(String[] args) {
        // Create a new instance of the Firefox driver
        // Notice that the remainder of the code relies on the interface,
        // not the implementation.
        WebDriver driver = new FirefoxDriver();
        // And now use this to visit Google
        driver.get("http://www.google.com");
        // Alternatively the same thing can be done like this
        // driver.navigate().to("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());
        // Google's search is rendered dynamically with JavaScript.
        // Wait for the page to load, timeout after 10 seconds
        (new WebDriverWait(driver, 10)).until(new ExpectedCondition<Boolean>() {
            public Boolean apply(WebDriver d) {
                return d.getTitle().toLowerCase().startsWith("cheese!");
            }
        });
        // Should see: "cheese! - Google Search"
        System.out.println("Page title is: " + driver.getTitle());
        // Close the browser
        driver.quit();
    }

}

I can't understand why this couldn't find main class...

Even more it couldn't create executable files .class into bin folder.

Any suggestion?

  • Why this exactly happen and how to solve this trouble?

删除 selenium 独立 jar 并将selenium-java-2.33.0-srcs.jarselenium-java-2.33.0.jar 添加到 lib 并作为java 应用程序运行

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM