服务器端使用selenium的例外情况

时间:2013-10-18 09:39:54

标签: java ajax servlets selenium selenium-webdriver

我有一个Web应用程序,要求用户输入URL。然后,我使用Selenium 2.35.0从URL中获取源代码。

我创建了一个dynamic web project,所有设置都正确(包括所有selenium jar),但我的应用程序抛出异常

Oct 18, 2013 2:49:48 PM org.apache.catalina.core.StandardWrapperValve invoke
SEVERE: Servlet.service() for servlet [DiffReportServlet] in context with path          [/PBSkyScraper] threw exception [Servlet execution threw an exception] with root cause
java.lang.ClassNotFoundException: org.openqa.selenium.WebDriver
at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1714)
at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1559)
at com.PB.automation.servlets.DiffReportServlet.doGet(DiffReportServlet.java:38)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:621)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:305)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:222)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:123)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:502)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:171)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:99)
at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:953)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:408)
at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1023)
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:589)
at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:312)
at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:895)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:918)
at java.lang.Thread.run(Thread.java:680)

这就是工作流程:

用户输入网址并提交。调用一个servlet,它实际上负责获取源代码。我无法理解原因,因为如果我尝试将其作为Java应用程序运行而不是在服务器上运行,则同样有效。

这是代码

 /**
 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
 */
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    String webDir = request.getServletContext().getRealPath("/WebContent");
    String url = request.getParameter("url");
    ApplicationUtils.getHTMLSourceFromURL(url, "originalSource.html");

}

public static void getHTMLSourceFromURL(String url, String originalFile) {

    WebDriver driver = new FirefoxDriver();
    driver.get(url);

    try {
        Thread.sleep(5000);

        List<String> pageSource = new ArrayList<String>(Arrays.asList(driver.getPageSource().split("\n")));

        writeTextToFile(pageSource, originalFile);

    } catch (InterruptedException e) {
        e.printStackTrace();
    }

    System.out.println("quitting webdriver");
    driver.quit();
}

有人可以解释原因并为此提供一些解决方案吗?我使用Tomcat作为服务器。

1 个答案:

答案 0 :(得分:1)

我在探索this answer之后终于完成了自己的工作。我刚删除了独立的jar并使用selenium-java-2.35.0.jar将lib文件夹移动到WEB-INF目录,一切正常。