how to resolve htmlUnit WrapsDriver Error

2019-08-25 08:04发布

问题:

I'm running test with HtmlUnit with selenium 3.13 jar, browser launches successfully, but after than it stops working with below error.

> Exception in thread "main" java.lang.NoClassDefFoundError: org/openqa/selenium/WrapsDriver
    at java.lang.ClassLoader.defineClass1(Native Method)
    at java.lang.ClassLoader.defineClass(Unknown Source)
    at java.security.SecureClassLoader.defineClass(Unknown Source)
    at java.net.URLClassLoader.defineClass(Unknown Source)
    at java.net.URLClassLoader.access$100(Unknown Source)
    at java.net.URLClassLoader$1.run(Unknown Source)
    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 org.openqa.selenium.htmlunit.HtmlUnitDriver.toWebElement(HtmlUnitDriver.java:1211)
    at org.openqa.selenium.htmlunit.HtmlUnitDriver.findElementByName(HtmlUnitDriver.java:1341)
    at org.openqa.selenium.By$ByName.findElement(By.java:284)
    at org.openqa.selenium.htmlunit.HtmlUnitDriver$4.call(HtmlUnitDriver.java:2024)
    at org.openqa.selenium.htmlunit.HtmlUnitDriver$4.call(HtmlUnitDriver.java:2020)
    at org.openqa.selenium.htmlunit.HtmlUnitDriver.implicitlyWaitFor(HtmlUnitDriver.java:1660)
    at org.openqa.selenium.htmlunit.HtmlUnitDriver.findElement(HtmlUnitDriver.java:2020)
    at org.openqa.selenium.htmlunit.HtmlUnitDriver.findElement(HtmlUnitDriver.java:798)
    at com.directlegalmail.startup.Startup.scrapDates(Startup.java:89)
    at com.directlegalmail.startup.Startup.main(Startup.java:63)
Caused by: java.lang.ClassNotFoundException: org.openqa.selenium.WrapsDriver
    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)
    ... 22 more

does anyone knows how to resolve it, I have selenium 3.13 and htmlUnit Driver 2.33

below is my code

driver = new HtmlUnitDriver();
driver.manage().window().maximize();
driver.manage().timeouts().implicitlyWait
(10000,TimeUnit.MILLISECONDS);
logMsg("Browser launched successfully");
driver.get("WebURL");

回答1:

you need to use htmlUnit Driver with dependencies, download the latest htmlunit-driver-x.xx.x-jar-with-dependencies.jar from github which include WrapsDriver class.



回答2:

Some more information about your Test Environment would have given us some more idea what exactly going wrong.

However I don't see any major issues in your code block. With Selenium v3.14 and HtmlunitDriver v2.33.0 while invoking HtmlUnitDriver you need to pass the argument true to enable JavaScript and you can use the following solution:

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

public class A_HtmlunitDriver_2_33_0 {

    public static void main(String[] args) throws InterruptedException {

    WebDriver driver = new HtmlUnitDriver(true);
    driver.manage().window().maximize();
    driver.get("https://stackoverflow.com/questions/53812207/how-to-resolve-htmlunit-wrapsdriver-error");
    System.out.println("HtmlUnitDriver invoked");
    driver.quit();
    }
}