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 present. I am using selenium-java 3.141.59 and htmlunit-driver 2.33.3 Here is my code
HtmlUnitDriver 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");
driver.getPageSource();
I tried it on a stack overflow page, and in the DOM get from the driver there is a tag saying that "javascript is not enabled"
<noscript>
<div id="noscript-warning">Stack Overflow works best with JavaScript enabled
<img src="https://pixel.quantserve.com/pixel/p-c1rF4kxgLUzNc.gif" alt="" class="dno">
</div>
</noscript>
I tried different Browsers and ways to enable javascript but nothing happened
HTML
<noscript>
TagThe
<noscript>
tag defines an alternate content for users that have disabled scripts in their browser or have a browser that doesn't support script. The element can be used in both and . When used inside the element: must contain only , , and elements. The content inside the element will be displayed if scripts are not supported, or are disabled in the user's browser. The element can be used in both and . When used inside the element: must contain only , , and elements. The content inside the element will be displayed if scripts are not supported, or are disabled in the user's browser.When I tried your code block:
I got an error at the line:
As:
There are two solutions:
Either cast the driver instance and invoke
setJavascriptEnabled(true)
as follows:Or pass the argument true which enables javascript support as follows:
In both the cases the output is identical as follows: