我使用下面的代码来获取Google搜索结果的前两页 但我只能获取第一页(当搜索第2页时,它与第1页相同)
import com.gargoylesoftware.htmlunit.WebClient;
import com.gargoylesoftware.htmlunit.html.HtmlElement;
import com.gargoylesoftware.htmlunit.html.HtmlPage;
import com.gargoylesoftware.htmlunit.html.HtmlTextInput;
/**
* A simple Google search test using HtmlUnit.
*
* @author Rahul Poonekar
* @since Apr 18, 2010
*/
public class Author_search {
static final WebClient browser;
static {
browser = new WebClient();
browser.setJavaScriptEnabled(false);
}
public static void main(String[] arguments) {
searchTest();
}
private static void searchTest() {
HtmlPage currentPage = null;
try {
currentPage = (HtmlPage) browser.getPage("http://www.google.com");
} catch (Exception e) {
System.out.println("Could not open browser window");
e.printStackTrace();
}
System.out.println("Simulated browser opened.");
try {
((HtmlTextInput) currentPage.getElementByName("q")).setValueAttribute("xxoo");
currentPage = currentPage.getElementByName("btnG").click();
System.out.println("contents: " + currentPage.asText());
HtmlElement next = (HtmlElement)currentPage.getByXPath("//span[contains(text(), 'Next')]").get(0);
currentPage = next.click();
System.out.println("contents: " + currentPage.asText());
} catch (Exception e) {
System.out.println("Could not search");
e.printStackTrace();
}
}
}
任何人都可以告诉我如何解决这个问题吗?
顺便说一下:答案 0 :(得分:2)
我已取代:
HtmlElement next = (HtmlElement)currentPage.getByXPath("//span[contains(text(),'Next')]").get(0);
currentPage = next.click();
<强>与强>
HtmlAnchor nextAnchor =currentPage.getAnchorByText("Next");
currentPage = nextAnchor.click();