is it possible to load a HtmlPage from a string?

谁说我不能喝 提交于 2019-12-06 04:53:07

问题


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)?


回答1:


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());



回答2:


I assume you're using HtmlParser.parseHtml to create the HtmlPage object and just need a WebResponse to pass to it?

If so, StringWebResponse will wrap your string so you can pass it directly to parseHtml.




回答3:


uhhmm well yes.

You just need to serve it ( if you're using java you can use Tomcat for that ) and point your test to the served page.



来源:https://stackoverflow.com/questions/2016762/is-it-possible-to-load-a-htmlpage-from-a-string

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!