Simple test with selenium HTMLUnitDriver do not work fine, is it wrong?

6.3k views Asked by At

I am a beginner with selenium, I am doing a test but I do not understand why it does not work. Here are my code and the following error.

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

And the error:

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 info: driver.version: HtmlUnitDriver at org.openqa.selenium.htmlunit.HtmlUnitDriver.findElementByName(HtmlUnitDriver.java:1293) at org.openqa.selenium.By$ByName.findElement(By.java:303) at org.openqa.selenium.htmlunit.HtmlUnitDriver$4.call(HtmlUnitDriver.java:1970) at org.openqa.selenium.htmlunit.HtmlUnitDriver$4.call(HtmlUnitDriver.java:1) at org.openqa.selenium.htmlunit.HtmlUnitDriver.implicitlyWaitFor(HtmlUnitDriver.java:1606) at org.openqa.selenium.htmlunit.HtmlUnitDriver.findElement(HtmlUnitDriver.java:1966) at org.openqa.selenium.htmlunit.HtmlUnitDriver.findElement(HtmlUnitDriver.java:754) at com.villegas.pruebas.PruebasRegresion.PruebaHTMLUnit.main(PruebaHTMLUnit.java:20)

3

There are 3 answers

3
Zakaria Shahed On

Please try this one hope it will works

 import org.openqa.selenium.*;
 import org.openqa.selenium.WebDriver;
 import org.openqa.selenium.WebElement;
 import org.openqa.selenium.htmlunit.HtmlUnitDriver;
    /**
     * Created by W3E64 on 9/15/2017.
     */
    public class testing_solution
    {
public static void main(String[] args) {
                java.util.logging.Logger.getLogger("com.gargoylesoftware.htmlunit").setLevel(java.util.logging.Level.OFF);
                java.util.logging.Logger.getLogger("org.apache.http").setLevel(java.util.logging.Level.OFF);
 HtmlUnitDriver unitDriver = new HtmlUnitDriver();

 unitDriver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
 unitDriver.get("http://google.com");
 System.out.println("Title of the page is -> " + unitDriver.getTitle());

 WebElement searchBox = unitDriver.findElement(By.name("q"))
 searchBox.sendKeys("Selenium");
 WebElement button = unitDriver.findElement(By.name("btnG"));
 button.click();
 System.out.println("Title of the page is -> " + unitDriver.getTitle());
            }
    }
6
iamsankalp89 On
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 was a part of Selenium version 2.53. If you are using Selenium 2.52 or earlier you don't need to download and install HtmlUnitDriver, it is already there.

But still you want to use it download it from here

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

From 2.53 version, you need to add it separately

See this image Image of result

Try this:

<!-- 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>
0
Prashant Prakash On

Instead of using submit() method use either a click on the google search button or input an Enter key in the search box. I have tried running the code above and it worked fine. I am using Selenium 3.141.59 with HtmlUnitDriver 2.36.0. The behaviour of HtmlUnitDriver varies with versions. The second method is a lot simpler and I personally prefer that:

element.sendKeys(Keys.ENTER);

Write this instead of submit and it will work fine.