'How to do HtmlPage refresh using HTMLUNIT Java?
I am facing an issue while refreshing the page. I am using HTMLUNIT.I am using WebClient and HTMLPAge to access a resource.
com.gargoylesoftware.htmlunit.WebClient
com.gargoylesoftware.htmlunit.HtmlPage
WebClient webClient = new WebClient()
webClient.setJavaScriptEnabled(true)
HtmlPage page = (HtmlPage)webClient.getPage(resource)
I need to refresh the page in order to get the new data from the resource. I tried to clear the cache webClient.getCache().clear(); and also tried to access the resource again with
HtmlPage page = (HtmlPage)webClient.getPage(resource)`.
I am getting the same previous data from the resource. I am beginner, can anyone please guide me how to do that.
Thanks, Naga
Solution 1:[1]
Did you try this simple one?
page.refresh();
Should do the work.
Solution 2:[2]
I had a similar problem some months ago and I did solve it by clearing the cache. Make sure you're using the appropriate variables. Give this code a try (correct any syntactical errors):
public static HtmlPage myGetPage(WebClient webClient, String url,
boolean clearCache) throws Exception {
if (clearCache) {
webClient.getCache().clear();
}
return webClient.getPage(url);
}
public void myMethod() {
WebClient webClient = new WebClient();
webClient.setJavaScriptEnabled(true);
String url = "http://stackoverflow.com/users/268273";
HtmlPage page = myGetPage(webClient, url, false); // We don't clear de cache
System.out.println(page.asXml()); // Should return original value
page = myGetPage(webClient, url, true); // We clear the cache
System.out.println(page.asXml()); // Should return the new value
}
PS: Of course, use your URL :)
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Sergey Vyacheslavovich Brunov |
Solution 2 | Mosty Mostacho |