0

I am trying to convert my selenium test from Firefox browser to HTMLUnit driver. But, when I try to run the HTMLUnit test , it gives me error for XPATH. The Firefox browser test runs absolutely fine.

My application test suite extensively uses XPATH . Hence, I am intentionally trying out with XPATH.

I have already tried using

new WebDriverWait(driver, 10)).until(ExpectedConditions.presenceOfElementLocated

but still I got the same error.

This is the error :

Exception in thread "main" org.openqa.selenium.NoSuchElementException: Unable to locate a node using .//*[@id='tsf']/div[2]/div[3]/center/input[1]
For documentation on this error, please visit: http://seleniumhq.org/exceptions/no_such_element.html
Build info: version: '2.48.2', revision: '41bccdd', time: '2015-10-09 19:55:52'
System info: host: 'WL309476', ip: '10.83.16.25', os.name: 'Windows 7', os.arch: 'x86', os.version: '6.1', java.version: '1.8.0_66'
Driver info: driver.version: HtmlUnitDriver
    at org.openqa.selenium.htmlunit.HtmlUnitDriver.findElementByXPath(HtmlUnitDriver.java:1161)
    at org.openqa.selenium.By$ByXPath.findElement(By.java:361)
    at org.openqa.selenium.htmlunit.HtmlUnitDriver$5.call(HtmlUnitDriver.java:1715)
    at org.openqa.selenium.htmlunit.HtmlUnitDriver$5.call(HtmlUnitDriver.java:1)
    at org.openqa.selenium.htmlunit.HtmlUnitDriver.implicitlyWaitFor(HtmlUnitDriver.java:1363)
    at org.openqa.selenium.htmlunit.HtmlUnitDriver.findElement(HtmlUnitDriver.java:1711)
    at org.openqa.selenium.htmlunit.HtmlUnitDriver.findElement(HtmlUnitDriver.java:606)
    at seleniumtest.Test_Google.main(Test_Google.java:17)

This is my Firefox browser test :

WebDriver driver = new FirefoxDriver();
driver.get("https://www.google.co.in/");
WebElement e =driver.findElement(By.xpath(".//*[@id='tsf']/div[2]/div[3]/center/input[1]"));
System.out.println("The current element is " + e.getAttribute("value"));

This is my HtmlUnit test :

WebDriver driver = new HtmlUnitDriver(true);
driver.get("https://www.google.co.in/");
WebElement e =driver.findElement(By.xpath(".//*[@id='tsf']/div[2]/div[3]/center/input[1]"));
System.out.println("The current element is " + e.getAttribute("value"));

I don't think it is a duplicate as there is no javascript involved in my case. I just want to port a simple test from Firefox driver to HTMLUnit.

Parag Mahajan
  • 15
  • 1
  • 1
  • 4
  • Possible duplicate of [Selenium Form Submission](http://stackoverflow.com/questions/32724664/selenium-form-submission) – SiKing Jan 12 '16 at 19:24
  • I don't think it is a duplicate as there is no javascript involved in my case. I just want to port a simple test from Firefox driver to HTMLUnit. – Parag Mahajan Jan 13 '16 at 07:13

1 Answers1

0

Try to apply a little wait for the page to load and then try and locate your element. This is just to be sure that your code didn't execute while the page was still being loaded.

Try something like below,

WebDriver driver = new HtmlUnitDriver(true);
driver.get("https://www.google.co.in/");
Thread.sleep(5000);
WebElement e =driver.findElement(By.xpath(".//*  [@id='tsf']/div[2]/div[3]/center/input[1]"));
System.out.println("The current element is " + e.getAttribute("value"));

The Thread.sleep(); will wait for the specified time before executing the next line.

Check with this and see if your element is found or not.

AND

Also check if your web page or the element is dependent on JavaScript. HTMLUnit Driver can't handle JavaScript and lacks of features which normal browsers have.

IAmMilinPatel
  • 413
  • 1
  • 11
  • 19
  • HtmlUnit and the `HtmlUnitDriver` have an option to enable JavaScript handling (see docs). It might be worth a try, however note, that it has its own JS engine and results will very likely be different compared to "real" browsers. – qqilihq Jan 13 '16 at 09:32