1

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)

Rajan Mishra
  • 1,178
  • 2
  • 14
  • 30
mvillegas
  • 57
  • 2
  • 9

3 Answers3

0
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>
iamsankalp89
  • 4,607
  • 2
  • 15
  • 36
  • How can i know which selenium I'm using? I thnk org.seleniumhq.selenium selenium-java 3.5.1 – mvillegas Sep 15 '17 at 08:54
  • Right click on Project -->go to build path-->add external jars – iamsankalp89 Sep 15 '17 at 08:54
  • You are using 3.5.1 – iamsankalp89 Sep 15 '17 at 08:55
  • USe this org.seleniumhq.selenium selenium-htmlunit-driver 2.52.0 – iamsankalp89 Sep 15 '17 at 08:59
  • TRY THIS @mvillegas and let me know – iamsankalp89 Sep 15 '17 at 11:50
  • 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) – mvillegas Sep 15 '17 at 12:08
0

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());
            }
    }
Zakaria Shahed
  • 2,589
  • 6
  • 23
  • 52
  • Exception in thread "main" org.openqa.selenium.NoSuchElementException: Unable to locate element with name: q For documentation on this error, please visit: http://seleniumhq.org/exceptions/no_such_element.html Build info: version: '3.5.3', revision: 'a88d25fe6b', time: '2017-08-29T12:54:15.039Z' 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 – mvillegas Sep 15 '17 at 11:33
  • WebElement element = driver.findElement(By.class("gsfi")); use this line insted of name – Zakaria Shahed Sep 15 '17 at 11:35
  • Driver info: driver.version: HtmlUnitDriver at org.openqa.selenium.htmlunit.HtmlUnitDriver.findElementByCssSelector(HtmlUnitDriver.java:1252) at org.openqa.selenium.htmlunit.HtmlUnitDriver.findElementByClassName(HtmlUnitDriver.java:1224) at org.openqa.selenium.By$ByClassName.findElement(By.java:391) – mvillegas Sep 15 '17 at 11:53
0

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.

Prashant Prakash
  • 105
  • 1
  • 10