Here is my problem, I\'m trying to load my website page for testing it but when i look at the html I get from the HtmlUnitDriver, elements displayed with javascript are not
TagThe
WebDriver driver = new HtmlUnitDriver();
driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
driver.setJavascriptEnabled(true);
driver.get("https://stackoverflow.com/questions/7926246/why-doesnt-htmlunitdriver-execute-javascript");
System.out.println(driver.getPageSource());
I got an error at the line:
driver.setJavascriptEnabled(true);
As:
The method setJavascriptEnabled(boolean) is undefined for the type WebDriver
There are two solutions:
Either cast the driver instance and invoke setJavascriptEnabled(true) as follows:
((HtmlUnitDriver) driver).setJavascriptEnabled(true);
Or pass the argument true which enables javascript support as follows:
WebDriver driver = new HtmlUnitDriver(true);
driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
driver.get("https://stackoverflow.com/questions/7926246/why-doesnt-htmlunitdriver-execute-javascript");
System.out.println(driver.getPageSource());
driver.quit();
In both the cases the output is identical as follows:
java - Why doesn't HtmlUnitDriver execute JavaScript? - Stack Overflow
<style>body,.top-bar{margin-top:1.9em}</style>