简体   繁体   中英

htmlunit execute page javascript

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?

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:

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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM