在Web服务中使用selenium会抛出java.lang.NoClassDefFoundError异常

时间:2013-05-21 01:28:06

标签: java web-services selenium webserver selenium-webdriver

我尝试从动态Web项目实现Web服务。我将selenium-server-standalone-2.32.0.jar文件添加到buildpath中,然后我还将它添加到WEB-INF / lib文件夹中。然后我使用Web服务向导从项目生成Web服务。在向导开始时,它显示一个弹出警告,显示为:

The service class "test.eko3.TestEko3" does not comply to one or more requirements of the JAX-RPC 1.1 specification, and may not deploy or function correctly.
The value type "org.openqa.selenium.WebDriver" used via the service class "test.eko3.TestEko3" does not have a public default constructor. Chapter 5.4 of the JAX-RPC 1.1 specification requires a value type to have a public default constructor, otherwise a JAX-RPC 1.1 compliant Web service engine may be unable to construct an instance of the value type during deserialization.
The field or property "windowHandles" on the value type "org.openqa.selenium.WebDriver" used via the service class "test.eko3.TestEko3" has a data type, "java.util.Set", that is not supported by the JAX-RPC 1.1 specification. Instances of the type may not serialize or deserialize correctly. Loss of data or complete failure of the Web service may result.

我点击确定,它生成了Web服务器和客户端,并在eclipse的浏览器中显示了客户端。但是当我输入参数并单击调用时,它在结果部分中显示了此异常:

Exception: java.lang.NoClassDefFoundError: org/openqa/selenium/WebDriver; nested exception is: java.lang.NoClassDefFoundError: org/openqa/selenium/WebDriver Message: java.lang.NoClassDefFoundError: org/openqa/selenium/WebDriver; nested exception is: java.lang.NoClassDefFoundError: org/openqa/selenium/WebDriver 

由于我将selenium jar添加到buildpath和WEB-INF / lib文件夹中,我不确定它为什么找不到该类。服务器的代码如下:

package test.eko3;

import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.support.ui.ExpectedCondition;
import org.openqa.selenium.support.ui.Wait;
import org.openqa.selenium.support.ui.WebDriverWait;

public class TestEko3 {
public String Ekobilet(String from, String to, String date) {

    //Firefox browser instantiation
    WebDriver driver = new FirefoxDriver();

    //Loading the URL
    driver.get("http://www.amadeusepower.com/trek/portals/trek/default.aspx?Culture=en-US");


    WebElement radioOneway = driver.findElement(By.id("ctl00_ctl00_ctl00_cph1_cph1_QuickSearchAll1_QuickFlightSearchControl1_rbFlightType_1"));
    radioOneway.click();

    waitForPageLoaded(driver);


    WebElement fromText = driver.findElement(By.id("ctl00_ctl00_ctl00_cph1_cph1_QuickSearchAll1_QuickFlightSearchControl1_txtSearch_txtFrom"));
    fromText.clear();
    fromText.sendKeys(from); 


    WebElement toText = driver.findElement(By.id("ctl00_ctl00_ctl00_cph1_cph1_QuickSearchAll1_QuickFlightSearchControl1_txtSearch_txtTo"));
    toText.sendKeys(to); 


    WebElement dateText = driver.findElement(By.id("ctl00_ctl00_ctl00_cph1_cph1_QuickSearchAll1_QuickFlightSearchControl1_txtDepartureDate_txtDate"));
    dateText.sendKeys(date); 

    //Sign in button identification and click it
    WebElement searchbutton = driver.findElement(By.id("ctl00_ctl00_ctl00_cph1_cph1_QuickSearchAll1_QuickFlightSearchControl1_btnSearch"));
    searchbutton.click();

    String page = driver.getPageSource();

//      Writer out = new BufferedWriter(new OutputStreamWriter(
//                  new FileOutputStream("ekobiletselenium.html"), "UTF-8"));
//              try {
//                  out.write(page);
//              } finally {
//                  out.close();
//              }
    //Closing the browser
    driver.close();

    return page;

    }

    public static void waitForPageLoaded(WebDriver driver) {

        ExpectedCondition<Boolean> expectation = new
    ExpectedCondition<Boolean>() {
           public Boolean apply(WebDriver driver) {
             return ((JavascriptExecutor)driver).executeScript("return document.readyState").equals("complete");
           }
         };

        Wait<WebDriver> wait = new WebDriverWait(driver,30);
         try {
                 wait.until(expectation);
         } catch(Throwable error) {
                 System.out.println("exception yavrum");
         }
    } 

}

有人可以告诉我原因吗?我错过了selenium所依赖的jar文件吗?任何帮助将不胜感激。

3 个答案:

答案 0 :(得分:0)

也许您正在使用对Web项目不利的独立jar。 见http://me-ol-blog.blogspot.co.il/2013/07/using-selenium-in-java-dynamic-web.html

答案 1 :(得分:0)

当项目中没有完全引用selenium时会出现此异常。

以下是解决问题的步骤:

  1. 下载Selenium客户端&amp; WebDriver Java Bindings here
  2. 解压缩下载的文件。
  3. 将主要的“.jar”(例如:selenium-java-2.42.2.jar)复制到网络项目的WebContent/WEB-INF/lib目录。
  4. 同时将解压缩的selenium libs目录的{{>全部 .jar文件复制到网络项目的WebContent/WEB-INF/lib目录。
  5. 您现在应该能够在不获取java.lang.NoClassDefFoundError: org/openqa/selenium/WebDriver例外的情况下运行代码。

答案 2 :(得分:0)

请检查此链接,绝对可以获得@thedrs

回答的想法

http://me-ol-blog.blogspot.co.il/2013/07/using-selenium-in-java-dynamic-web.html