Amila
Amila

Reputation: 243

How to render an HTMLUnit page to a browser?

I am using HTMLUnit for site scraping purpose. I have a problem for displaying Html page in browser. It mean when I'm doing site scraping the site adding new pages. So I need to display that page to browser through my HTMLUnit application. So how to render an HTMLUnit page to a browser?

Upvotes: 0

Views: 2578

Answers (1)

Andrey
Andrey

Reputation: 925

As I understood your needs, you load the page with HtmlUnit and want to show it in browser after some manipulations on rendered code.

In general, it takes time to render the page, and I saw some solutions with sleep that I didn't like.

There are two methods:

  1. page.asText() - Shows rendered text
  2. page.asXml() - Shows text before rendering

But both functions are not what you are looking for.

One weird, but still working solution, is to save rendered page, and than load it with browser:

// Get page as Html
HtmlPage page = wc.getPage("http://stackoverflow.com/");

// Generate random file in temp directory
File file = File.createTempFile("HtmlUnit", ".html");
file.delete(); // Delete is needed, because page.save can't overwrite it

//save page image
page.save(file);

//Open the page with a browser
Runtime.getRuntime().exec("C:/Program Files/Internet Explorer/iexplore.exe " + file);

Upvotes: 2

Related Questions