Reputation: 37
I have a question.
What makes FirefoxDriver be able to locate WebElements and click on them in a java code but when running the same code with HtmlUnitDriver the same WebElements are not located. Also when running the same code on HtmlUnit(applying HtmlUnit principles) WebElements are not found, in fact the code return NullPointerException. Is there a particular reason?
Upvotes: 5
Views: 265
Reputation: 28742
Without seeing your code, I might hazard a guess that it might be because you need to enable javascript.
JavaScript is disabled by default in the HtmlUnitDriver.
If you look at the source constructor at(LICENSE Apache 2.0)
/**
* Constructs a new instance, specify JavaScript support
* and using the {@link BrowserVersion#getDefault() default} BrowserVersion.
*
* @param enableJavascript whether to enable JavaScript support or not
*/
public HtmlUnitDriver(boolean enableJavascript) {
this(BrowserVersion.getDefault(), enableJavascript);
}
And the other constructors at https://github.com/SeleniumHQ/htmlunit-driver/blob/master/src/main/java/org/openqa/selenium/htmlunit/HtmlUnitDriver.java#L143-L158
/**
* Constructs a new instance with JavaScript disabled,
* and the {@link BrowserVersion#getDefault() default} BrowserVersion.
*/
public HtmlUnitDriver() {
this(BrowserVersion.getDefault(), false);
}
/**
* Constructs a new instance with the specified {@link BrowserVersion}.
*
* @param version the browser version to use
*/
public HtmlUnitDriver(BrowserVersion version) {
this(version, false);
}
You see that they provide a default false if the variable isn't provided.
So to enable javascript in the HtmlUnitDriver you'll need to provide true when initializing it, that you want JavaScript components active in it.
WebDriver driver = new HtmlUnitDriver(BrowserVersion.FIREFOX_38, true);
Upvotes: 2