我是硒的初学者,正在做测试,但我不明白为什么它不起作用。
这是我的代码和以下错误。

 import org.openqa.selenium.*;
 import org.openqa.selenium.WebDriver;
 import org.openqa.selenium.WebElement;
 import org.openqa.selenium.htmlunit.HtmlUnitDriver;
 public class PruebaHTMLUnit {
    public static void main(String[] args) {

WebDriver driver = new HtmlUnitDriver();
driver.get("http://www.google.com");
WebElement element = driver.findElement(By.name("q"));
element.sendKeys("Guru99");
element.submit();
System.out.println("Page title is: " + driver.getTitle());

driver.quit();
}
}


错误:


Build info: version: '3.5.2', revision: '10229a9', time: '2017-08-21T17:29:55.15Z'
System info: host: 'MVILLEGAS764', ip: '169.254.106.219', os.name: 'Windows 7', os.arch: 'amd64', os.version: '6.1', java.version:

  
  '1.8.0_144'
      驱动程序信息:driver.version:HtmlUnitDriver
        在org.openqa.selenium.htmlunit.HtmlUnitDriver.findElementByName(HtmlUnitDriver.java:1293)
        在org.openqa.selenium.By $ ByName.findElement(By.java:303)
        at org.openqa.selenium.htmlunit.HtmlUnitDriver $ 4.call(HtmlUnitDriver.java:1970)
        在org.openqa.selenium.htmlunit.HtmlUnitDriver $ 4.call(HtmlUnitDriver.java:1)
        在org.openqa.selenium.htmlunit.HtmlUnitDriver.implicitlyWaitFor(HtmlUnitDriver.java:1606)
        在org.openqa.selenium.htmlunit.HtmlUnitDriver.findElement(HtmlUnitDriver.java:1966)
        在org.openqa.selenium.htmlunit.HtmlUnitDriver.findElement(HtmlUnitDriver.java:754)
        在com.villegas.pruebas.PruebasRegresion.PruebaHTMLUnit.main(PruebaHTMLUnit.java:20)

最佳答案

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

    public class test_Stackoverflow
    {
            public static void main(String[] args) {
                       WebDriver driver = new HtmlUnitDriver();
                driver.get("https://www.google.com");
                WebElement element = driver.findElement(By.xpath(".//*[@id='gs_htif0']"));
                element.sendKeys("Guru99");
                element.submit();
                System.out.println("Page title is: " + driver.getTitle());
                driver.quit();
            }
    }


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

但是仍然要使用它,请从此处下载

https://github.com/SeleniumHQ/htmlunit-driver/releases

从2.53版本开始,您需要单独添加

看到此图像java - 用 Selenium HTMLUnitDriver进行的简单测试无法正常工作,这是错误的吗?-LMLPHP

尝试这个:

<!-- https://mvnrepository.com/artifact/org.seleniumhq.selenium/selenium-htmlunit-driver -->
<dependency>
    <groupId>org.seleniumhq.selenium</groupId>
    <artifactId>selenium-htmlunit-driver</artifactId>
    <version>2.52.0</version>
</dependency>

09-25 20:16