In my example, the page a.html will navigate to a new page(http://www.google.com/) automatically in 2 seconds.
At first, I load page a.html in to a WebBrowser instance:browser, but then I can't use browser.getContent() to get the content
of google's index page?
How to solve it? Thanks a lot!
The code is available:
Hi Hegh,
The problem is that you use browser.waitReady() method.
In your case it waits till the first page is loaded and then sample code shows its content. But loaded page redirects to www.google.com only in 3 seconds.
To solve the problem you can use the NavigationEventListener.entireDocumentCompleted(WebBrowser browser, String s) method and call to browser.getContent() in it, for example:
browser.addNavigationListener(new NavigationEventAdapter() {
public void entireDocumentCompleted(WebBrowser webBrowser, String s) {
System.out.println(s);
//contains is used here because google.com redirects itself to other page
if (s.contains("www.google.com")) {
System.out.println(webBrowser.getContent());
}
}});
Regards,
Slava
Hi hegh,
Please do not just reply to the email notification from the forum, but also, if it's possible, post your reply to this forum. Thanks. Here is your message:
Thank you very much for your reply.
I also find a solution: getting the redirection time t in the page's meta tag and then using Thread.sleep(t) after browser.waitReady(). But I think your method is much better. Mine can not solve the problems that pages finish redirection by javascripts. Yours can do it. Would you please do me a favor to solve another problem? The source code is below:
WebBrowser browser = new IEAutomation();
((IEAutomation)browser).setVisible(true);
browser.navigate("http://www.giannifrey.com/ajax/news.html");
browser.waitReady();
Thread.sleep(5000);
HTMLElement element = (HTMLElement)browser.getDocument().getElementById("btn_next");
element.click();
browser.addNavigationListener(new NavigationEventAdapter() {
public void documentCompleted(WebBrowser webBrowser, String url) {
System.out.println(url);
String c = webBrowser.getContent();
//to find whether the browser has loaded the second article' content(contains "willkommen").
//if loaded, print it.
if (c.contains("willkommen")) {
System.out.println(c);
}
}
});
I take your solution in the above code, but I can never prints the second article's content.
Here supplies some additional information:
1.The site http://www.giannifrey.com/ajax/news.html is developed by ajax.I think this is very important.
2.I use Thread.sleep(5000) to load the first page's content into browser and then I can
click on the button ">"(represents by the id:btn_next) to navigate to the page that
shows the second article's content.
3.All the pages in this site uses the same url.This is also very important.
Would you please do me a favor to print the second article's content not by using "Thread.sleep()"?
To make generalization, two pages(page a, page b) are developed by ajax and use the same url.
By clicking a button on page a, you can navigate to page b. How can you get the page b's content after
firing onclick event on page a by using addNavigationListener or other eventListener?
Thanks very much again.
Hi,
Unfortunately in this case there's no way to get page B's content after firing onclick event on page A without using "Thread.sleep()". The reason of this issue is that when you click Next button on page A then page B is loaded with using AJAX request. But the documentCompleted event isn't fires when AJAX request has occurs. And current implementation of JExplorer doesn't allow listening AJAX request events. So in order to make sure that page B is loaded completely you need to use the Thread.sleep() method.
Regards,
Vladimir Ikryanov