2

All of the examples I see for htmlunit retrieve the HTML from website via a URL using getPage.

WebClient webClient = new WebClient();
HtmlPage page = webClient.getPage("http://htmlunit.sourceforge.net");

I'm holding an HTML fragment in a string variable and can't find a way to give it the HTML from a variable.

I've tried casting the String to HtmlPage but that doesn't work.

HtmlPage page = (HtmlPage) htmlFragment

1 Answer 1

3

Perhaps you could write the HTML fragment (possibly wrapped in the standard html/head/body tags) into a temp file and use its URL for your web client. For example (untested):

File tempFile = File.createTempFile("fragment", "html");
HtmlPage startPage = webClient.getPage(tempFile.toURI().toURL().toString());
tempFile.deleteOnExit();

Alternatively, you might be able to construct your own HtmlPage with a phony URL and ad-hoc WebResponse and WebWindow but that might require a lot of hacking.

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

1 Comment

Thanks, this may be my best alternative.

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.