0

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

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

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)

这就是工作流程:

用户输入 URL 并提交。一个 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 作为服务器。

4

1 回答 1

1

在探索了这个答案后,我终于自己解决了。我刚刚删除了独立的 jar,并使用selenium-java-2.35.0.jar了将 lib 文件夹移动到WEB-INF目录,一切正常。

于 2013-10-21T08:32:24.367 回答