如何使用htmlunit获取修改后的ajax内容
问题描述:
我想与您分享如何检索由ajax更改的html页面的内容。如何使用htmlunit获取修改后的ajax内容
以下代码返回旧页面。
public class Test {
public static void main(String[] args) throws FailingHttpStatusCodeException, MalformedURLException, IOException, InterruptedException {
String url = "valid html page";
WebClient client = new WebClient(BrowserVersion.FIREFOX_17);
client.getOptions().setJavaScriptEnabled(true);
client.getOptions().setRedirectEnabled(true);
client.getOptions().setThrowExceptionOnScriptError(true);
client.getOptions().setCssEnabled(true);
client.getOptions().setUseInsecureSSL(true);
client.getOptions().setThrowExceptionOnFailingStatusCode(false);
client.setAjaxController(new NicelyResynchronizingAjaxController());
HtmlPage page = client.getPage(url);
System.out.println(page.getWebResponse().getContentAsString());
}
}
这到底是怎么回事?
答
答案是page.getWebResponse()赋予初始页面。
为了得到更新的内容,我们必须使用页面变量本身
package utils;
import java.io.IOException;
import java.net.MalformedURLException;
import com.gargoylesoftware.htmlunit.BrowserVersion;
import com.gargoylesoftware.htmlunit.FailingHttpStatusCodeException;
import com.gargoylesoftware.htmlunit.NicelyResynchronizingAjaxController;
import com.gargoylesoftware.htmlunit.WebClient;
import com.gargoylesoftware.htmlunit.html.HtmlPage;
public class Test {
public static void main(String[] args) throws FailingHttpStatusCodeException, MalformedURLException, IOException, InterruptedException {
String url = "valid html page";
WebClient client = new WebClient(BrowserVersion.FIREFOX_17);
client.getOptions().setJavaScriptEnabled(true);
client.getOptions().setRedirectEnabled(true);
client.getOptions().setThrowExceptionOnScriptError(true);
client.getOptions().setCssEnabled(true);
client.getOptions().setUseInsecureSSL(true);
client.getOptions().setThrowExceptionOnFailingStatusCode(false);
client.setAjaxController(new NicelyResynchronizingAjaxController());
HtmlPage page = client.getPage(url);
System.out.println(page.asXml());
System.out.println(page.getWebResponse().getContentAsString());
}
}
我发现暗示在以下链接
http://htmlunit.10904.n7.nabble.com/Not-expected-result-code-from-htmlunit-td28275.html
艾哈迈德Ashour雅虎你写的: 嗨,你不应该使用WebResponse,这意味着从 服务器获得实际内容。你应该使用htmlPage .asText()或.asXml()您的,Ahmed