如何解决htmlUnit WrapsDriver错误

时间:2018-12-17 09:23:53

标签: java selenium selenium-webdriver htmlunit-driver

我正在使用带有硒3.13 jar的HtmlUnit进行测试,浏览器成功启动,但是之后它停止工作并出现以下错误。

> 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

有人知道如何解决吗,我有 硒3.13 和htmlUnit驱动程序2.33

下面是我的代码

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

3 个答案:

答案 0 :(得分:1)

您需要使用具有依赖性的htmlUnit驱动程序,从github下载包含WrapsDriver类的最新htmlunit-driver-x.xx.x-jar-with-dependencies.jar

答案 1 :(得分:0)

有关您的测试环境的更多信息将使我们更加了解到底出了什么问题。

但是,我在您的代码块中看不到任何重大问题。对于 Selenium v​​3.14 HtmlunitDriver v2.33.0 ,在调用 HtmlUnitDriver 时,您需要传递参数 true 来启用 JavaScript ,您可以使用以下解决方案:

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();
    }
}

答案 2 :(得分:0)

这种依赖关系组合对我有用:

implementation("org.seleniumhq.selenium:selenium-java:3.141.59")
implementation("org.seleniumhq.selenium:selenium-api:3.141.59")
implementation("org.seleniumhq.selenium:htmlunit-driver:2.36.0")

您可以在以下位置查看与HtmlUnit驱动程序版本2.36.0兼容的Selenium依赖关系:https://github.com/SeleniumHQ/htmlunit-driver/blob/2.36.0/pom.xml