HtmlUnitDriver is unable to locate a node

webdriverxpath

I cannot get HtmlUnitDriver to find elements on a page I am trying to access. WebDriver on the other hand works fine. Both are using the same method.
Here is my code:

import java.util.logging.*;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.htmlunit.HtmlUnitDriver;
import com.gargoylesoftware.htmlunit.BrowserVersion;


public class Main {


    public static void main(String[]args)
    {
        FirefoxDriver driver2=new FirefoxDriver();

        HtmlUnitDriver driver=new HtmlUnitDriver(BrowserVersion.FIREFOX_10);
        driver.setJavascriptEnabled(true);
        Logger.getLogger("com.gargoylesoftware").setLevel(Level.OFF);

        runTest(driver2); //works
        runTest(driver);  //does not work
    }

    public static void runTest(WebDriver driver)
    {
        driver.get("http://10.3.1.164");
        WebElement elem=driver.findElement(By.xpath("//td[2]/input"));
        assert elem.isDisplayed();
        System.out.println(driver.getTitle());
    }
}

Here is the stacktrace:

Exception in thread "main" org.openqa.selenium.NoSuchElementException: Unable to     locate a node using //td[2]/input
For documentation on this error, please visit: http://seleniumhq.org/exceptions/no_such_element.html
Build info: version: '2.28.0', revision: '18309', time: '2012-12-11 20:21:45'
System info: os.name: 'Windows 7', os.arch: 'amd64', os.version: '6.1', java.version: '1.7.0_10'
Driver info: driver.version: HtmlUnitDriver
at     org.openqa.selenium.htmlunit.HtmlUnitDriver.findElementByXPath(HtmlUnitDriver.java:805)
at org.openqa.selenium.By$ByXPath.findElement(By.java:344)
at org.openqa.selenium.htmlunit.HtmlUnitDriver$5.call(HtmlUnitDriver.java:1247)
at org.openqa.selenium.htmlunit.HtmlUnitDriver$5.call(HtmlUnitDriver.java:1)
at org.openqa.selenium.htmlunit.HtmlUnitDriver.implicitlyWaitFor(HtmlUnitDriver.java:987)
at org.openqa.selenium.htmlunit.HtmlUnitDriver.findElement(HtmlUnitDriver.java:1244)
at org.openqa.selenium.htmlunit.HtmlUnitDriver.findElement(HtmlUnitDriver.java:393)
at Main.runTest(Main.java:28)
at Main.main(Main.java:22)

Best Solution

I figured it out. It was just a matter of me waiting for the elements to get loaded. Here's the code.

import java.io.IOException;
import java.net.MalformedURLException;
import java.util.logging.*;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.htmlunit.*;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
import com.gargoylesoftware.htmlunit.BrowserVersion;
import com.gargoylesoftware.htmlunit.FailingHttpStatusCodeException;

public class Main {


public static void main(String[]args) throws FailingHttpStatusCodeException, MalformedURLException, IOException
{
    FirefoxDriver driver2=new FirefoxDriver();

    HtmlUnitDriver driver=new HtmlUnitDriver(BrowserVersion.FIREFOX_3_6);

    driver.setJavascriptEnabled(true);
    Logger.getLogger("com.gargoylesoftware").setLevel(Level.OFF);

    runTest(driver2); //works
    runTest(driver);  //does not work
}

public static void runTest(WebDriver driver)
{
    driver.get("http://10.3.1.164");
    WebElement elem=(new WebDriverWait(driver, 10)) //added this line
    .until(ExpectedConditions.presenceOfElementLocated(By.xpath("//td[2]/input")));
    System.out.println(elem.getAttribute("id"));
    assert elem.isDisplayed();
    System.out.println(driver.getTitle());
}
}
Related Question