Alex Rivera | Logout

HtmlUnit to view source

Asked 2011-05-13T19:01:57.920
14

HtmlUnit for Java is great but I haven't been able to figure out how to view the full source or return the source of a web site as a string. can anyone help me with this?

I know the follow will read the site but now I just want to return the source to a string.

HtmlPage mySite = webClient.getPage("http://mysite.com");

Thanks!

Edit
Report

1 Answer

15
String pageSource = myPage.asXml();

That will get you the full HTML source of the web page.

String pageText = myPage.asText();

That will get you all of the visible text on the page, including line breaks/white space. It would be the same if you were on the page in your browser and Ctrl+A then Ctrl+V into a variable.

answered 2011-05-13T19:58:48.887

Your Answer