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.

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:    
Build info: version: '2.48.2', revision: '41bccdd', time: '2015-10-09   
System info: host: 'WL309476', ip: '', 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.By$ByXPath.findElement(By.java:361)
at org.openqa.selenium.htmlunit.HtmlUnitDriver$5.call(HtmlUnitDriver.java:1)
at seleniumtest.Test_Google.main(Test_Google.java:17)

This is my Firefox browser test :

WebDriver driver = new FirefoxDriver();
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);
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"));

Without XPATH it works fine , even on htmlunit driver. Problem happens when I have to use XPATH.

I have tried implicit timeout as well as sleep , but it did not work.

There is no dependency on Javascript as well.

When I tried with PhantomJS , I observed same behavior ie it does not work with XPATH.

Here is the PhantomJS code :

     File file = new File("C:/Parag/Software/phantomjs-2.0.0-windows/phantomjs-2.0.0-windows/bin/phantomjs.exe");  
     System.setProperty("phantomjs.binary.path", file.getAbsolutePath());
     WebDriver driver = new PhantomJSDriver();

     WebElement element = driver.findElement(By.xpath(".//*[@id='tsf']/div[2]/div[3]/center/input[1]"));

The above code works fine with Firefox driver but fails on PhantomJS.


3 Answers 3


Probably the elements you are looking for a updated at a later point in time. First the page is downloaded, then rendered and then the JavaScript is run. The element has not yet been created by the JavaScript. Maybe HTMLUnit does not process the JavaScript correctly.

HTMLUnit is not know for its JavaScript implementation. Its primary usage is unit-testing HTML, although it supports basic JavaScript I do not think you should use it for testing dynamic web applications.

As you are testing the Google website which uses JavaScript heavily I suggest you use another headless browser instead, namely PhantomJS. PhantomJS implements the Webkit render engine, which is also used by Google Chrome and Safari. This means its a "real" browser and implements most browser features.


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);
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.


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.


Use below line before getting particular element id. Hopefully it will resolve the problem.

WebElement elem=(new WebDriverWait(driver, 10)) //added this line
        .until(ExpectedConditions.presenceOfElementLocated(By.xpath("//input[@ type='email']")));

PS: this is to make driver wait unless xpath containing "email" is not rendered properly.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.