Getting error "java.lang.NoClassDefFoundError: com/gargoylesoftware/htmlunit/html/HtmlElement"

Getting error "java.lang.NoClassDefFoundError: com/gargoylesoftware/htmlunit/html/HtmlElement"

我正在使用 selenium webdriver 进行测试我编写了代码:

import java.util.concurrent.TimeUnit;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.firefox.MarionetteDriver;
import org.openqa.selenium.htmlunit.HtmlUnitDriver;
//import org.openqa.selenium.firefox.FirefoxProfile;
import org.testng.Assert;
import org.testng.annotations.AfterTest;
//import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;


public class sample_google
{

 WebDriver driver;


    @BeforeTest

    public  void setUp(){

       // FirefoxProfile profile = new FirefoxProfile();
     //System.setProperty("webdriver.gecko.driver", "D:\ashwini\geckodriver.exe");
     
//    driver= new MarionetteDriver();
      driver = new HtmlUnitDriver();

     driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
    }


    @Test

    public void testLoadingFirstPage() throws Exception{

        driver.get("https://www.google.com/");
        WebElement searchBox = driver.findElement(By.name("q"));
        searchBox.sendKeys("fast cars");
        searchBox.submit();
        System.out.println("Page title : " + driver.getTitle());
        WebElement resultCount = driver.findElement(By.id("resultStats"));
        System.out.println("Result Count : " + resultCount.getText());
       

    }
    @AfterTest
   public void teardown()
   {
    //driver.quit();
   }

    
}
但是我得到输出

FAILED CONFIGURATION: @BeforeTest setUp
java.lang.NoClassDefFoundError: com/gargoylesoftware/htmlunit/html/HtmlElement
 at sample_google.setUp(sample_google.java:31)
 at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
 at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
 at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
 at java.lang.reflect.Method.invoke(Unknown Source)
 at org.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:86)
 at org.testng.internal.Invoker.invokeConfigurationMethod(Invoker.java:514)
 at org.testng.internal.Invoker.invokeConfigurations(Invoker.java:215)
 at org.testng.internal.Invoker.invokeConfigurations(Invoker.java:142)
 at org.testng.TestRunner.beforeRun(TestRunner.java:656)
 at org.testng.TestRunner.run(TestRunner.java:624)
 at org.testng.SuiteRunner.runTest(SuiteRunner.java:366)
 at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:361)
 at org.testng.SuiteRunner.privateRun(SuiteRunner.java:319)
 at org.testng.SuiteRunner.run(SuiteRunner.java:268)
 at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:52)
 at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:86)
 at org.testng.TestNG.runSuitesSequentially(TestNG.java:1244)
 at org.testng.TestNG.runSuitesLocally(TestNG.java:1169)
 at org.testng.TestNG.run(TestNG.java:1064)
 at org.testng.remote.AbstractRemoteTestNG.run(AbstractRemoteTestNG.java:126)
 at org.testng.remote.RemoteTestNG.initAndRun(RemoteTestNG.java:152)
 at org.testng.remote.RemoteTestNG.main(RemoteTestNG.java:57)
Caused by: java.lang.ClassNotFoundException: com.gargoylesoftware.htmlunit.html.HtmlElement
 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)
 ... 23 more

SKIPPED CONFIGURATION: @AfterTest teardown
SKIPPED: testLoadingFirstPage

===============================================
    Default test
    Tests run: 1, Failures: 0, Skips: 1
    Configuration Failures: 1, Skips: 1
===============================================


===============================================
Default suite
Total tests run: 1, Failures: 0, Skips: 1
Configuration Failures: 1, Skips: 1
===============================================

[TestNG] Time taken by org.testng.reporters.jq.Main@2752f6e2: 142 ms
[TestNG] Time taken by [FailedReporter passed=0 failed=0 skipped=0]: 28 ms
[TestNG] Time taken by org.testng.reporters.EmailableReporter2@6e8dacdf: 14 ms
[TestNG] Time taken by org.testng.reporters.SuiteHTMLReporter@2d209079: 48 ms
[TestNG] Time taken by org.testng.reporters.XMLReporter@43a25848: 15 ms
[TestNG] Time taken by org.testng.reporters.JUnitReportReporter@b684286: 9 ms

我在代码中包含了 selenium-htmlunit-driver-2.46.0.jar 的 jar。 仍然收到错误。 所以请检查是什么问题。

提前致谢

我在库中单独包含了 selenium-htmlunit-driver-2.46.0.jar 的 jar。仍然收到错误

如果您使用的是 Selenium 主分发包版本 2.53 或更高版本,那么您应该单独添加 HtmlUnitDriver,它不再与独立的 jar 文件捆绑在一起。

如果您使用的是 Selenium 2.52 或更早版本,则无需下载和安装 HtmlUnitDriver,它已经存在。

请相应地进行更改,它应该可以正常工作。 Learn more.

将以下依赖项添加到您的 pom.xml 文件中,它应该可以正常工作

<groupId>org.seleniumhq.selenium</groupId>
<artifactId>htmlunit-driver</artifactId>
<version>2.20</version>
</dependency>

从 2.53 开始,webdriver 不再附带它