Search code examples
javahtmlunit

is it possible to load a HtmlPage from a string?


I have stored a webpage's HTML in the database.

I want to take advantage of HtmlUnit's ability to find/reference DOM elements.

Is it possible to load the HtmlPage object from a string (via a database column)?


Solution

  • StringWebResponse may help.

    Edit: example:

        URL url = new URL("http://www.example.com");
        StringWebResponse response = new StringWebResponse("<html><head><title>Test</title></head><body></body></html>", url);
        HtmlPage page = HTMLParser.parseHtml(response, new TopLevelWindow("top", new WebClient()));
        System.out.println(page.getTitleText());