4

I am trying get the content of a web page after the execution of the javascript code in the page. Let's suppose for example that I have the following page :

<html>
<body>
  test:
  <div id="inner"></div>
  <script type="text/javascript">
    document.getElementById('inner').innerHTML = "Hello World!";
</script>
</body>
</html>

what I want to extract is the page after the execution of the javascript, so the rendered html :

<html>
<body>
  test:
  <div id="inner">Hello World</div>
</script>
</body>
</html>

Is it possible in htmlUnit?

2

2 Answers 2

3

I'm not sure what issue you are having with that code but it works perfectly for me.

I created a file with that content and the result I got from fetching the content of the page was:

<?xml version="1.0" encoding="ISO-8859-1"?>
<html>
  <head/>
  <body>

  test:

    <div id="inner">
      Hello World!
    </div>
    <script type="text/javascript">
//<![CDATA[

    document.getElementById('inner').innerHTML = "Hello World!";

//]]>
    </script>
  </body>
</html>

This is all the code you need:

WebClient webClient = new WebClient();
HtmlPage page = webClient.getPage("the_url");
System.out.println(page.asXml());

You might also find this question useful:

Sign up to request clarification or add additional context in comments.

Comments

1

I hope I understood your question correctly. htmlUnit does support execution of JavaScript code.. Check out this tutorial it might help you get started.

Also if you're doing application testing in a professional enviroment, especially if it's on a larger scale, then I would like to advice you to use something a bit more advanced than htmlUnit, for example Selenium.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.