[HtmlUnit] [htmlunit:bugs] #2003 HTMLParser.parseHtml(htmlUnitResponse, webClient.getCurrentWindow()) is not parsing the javascripts in the htmlUnitResponse.

RBRi via HtmlUnit-develop <[email protected]> Mon, 03 Dec 2018 18:37:41 -0000
Newsgroups gmane.comp.java.htmlunit.devel
Message-ID </p/htmlunit/bugs/2003/a7fb91736077ab7d7478b33c937aaa79bb1ed004.bugs@htmlunit.p.sourceforge.net>
- **Comment**:

There are two options:

you already have a file (in the end many files for external js) you can simply use your file url

~~~~~
try (final WebClient webClient = new WebClient()) {
    HtmlPage page = webClient.getPage(rurl);
    System.out.print(page.asXml());
}
~~~~~


if you really like to work with a string you can do this:

~~~~~
URL url = new URL(rurl);
StringWebResponse htmlUnitResponse = new StringWebResponse(rhtml, url);

try (final WebClient webClient = new WebClient()) {
    WebWindow webWindow = webClient.getCurrentWindow();
    HtmlPage page = (HtmlPage) webClient.loadWebResponseInto(htmlUnitResponse, webWindow);
    webClient.getJavaScriptEngine().registerWindowAndMaybeStartEventLoop(webWindow);
    webClient.waitForBackgroundJavaScriptStartingBefore(1000);

    System.out.print(page.asXml());
}
~~~~~



---

** [bugs:#2003] HTMLParser.parseHtml(htmlUnitResponse, webClient.getCurrentWindow()) is not parsing the javascripts in the htmlUnitResponse.**

**Status:** open
**Group:** 2.33
**Created:** Mon Dec 03, 2018 11:52 AM UTC by Manikandan Kabali
**Last Updated:** Mon Dec 03, 2018 11:52 AM UTC
**Owner:** nobody


Hi All,
I am using htmlunit 2.33. When I pass some html string which has javascript to create  dynamic DOM elements to HTMLParser.parseHtml, it is not processing the javascript in the html string.
So, it is not creating the dynamic dom elements. But when i get the html page using the following piece of code it parses the js "HtmlPage page= webClient.getPage(url)". In my case, i don't want to get the page using "HtmlPage page= webClient.getPage(url)". I have the html text to be parsed. Could anyone suggest some solution to parse the js in the html string?

# **sample.html**
<html>
<head>
<title>Sample html to pdf</title>
<meta charset="utf-8"/>
<script  type="text/javascript" src="jquery/jquery-3.3.1.js"></script>
<script type="text/javascript">
jQuery(
    function($)
    {
        $("#dynamic").append("<p>I came via jquery!!</p>");
    }

    );
</script>
</head>
<body>
<div id="h1">
<center><h1>Sample HTML to PDF</h1></center>
</div>
<div id="dynamic">
</div>
</body>
</html>

# java code
		final String rhtml = readFileAsString("D:\\Print Pdf Works\\html\\sample_html_wjs.html");
		String rurl = "file:///D:/Print%20Pdf%20Works/html/sample_html_wjs.html";

				URL url = new URL(rurl);
				StringWebResponse htmlUnitResponse = new StringWebResponse(rhtml, url);
				try (WebClient webClient = new WebClient()) {
					webClient.getOptions().setJavaScriptEnabled(true);
					webClient.waitForBackgroundJavaScript(90000);
					webClient.waitForBackgroundJavaScriptStartingBefore(90000);
					HtmlPage page = HTMLParser.parseHtml(htmlUnitResponse, webClient.getCurrentWindow());

					System.out.println("************HTML PAGE**************");
					System.out.print(page.asXml());
					renderer.setDocument(page, urlString);
				}
# output
************HTML PAGE**************
<?xml version="1.0" encoding="UTF-8"?>
<html>
  <head>
    <title>
      Sample html to pdf
    </title>
    <meta charset="utf-8"/>
    <script type="text/javascript" src="jquery/jquery-3.3.1.js">
    </script>
    <script type="text/javascript">
//<![CDATA[

jQuery(
    function($)
    {
        $("#dynamic").append("<p>I came via jquery!!</p>");
    }

    );

//]]>
    </script>
  </head>
  <body>
    <div id="h1">
      <center>
        <h1>
          Sample HTML to PDF
        </h1>
      </center>
    </div>
    <div id="dynamic">
    </div>
  </body>
</html>



---

Sent from sourceforge.net because [email protected] is subscribed to https://sourceforge.net/p/htmlunit/bugs/

To unsubscribe from further messages, a project admin can change settings at https://sourceforge.net/p/htmlunit/admin/bugs/options.  Or, if this is a mailing list, you can unsubscribe from the mailing list.

_______________________________________________
HtmlUnit-develop mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/htmlunit-develop