我正在嘗試使用htmlUnit來執行javascript。執行javascript後,我想檢查頁面是否因JavaScript執行而發生了一些變化。即我想之前和之後執行JavaScript的HTML頁面比較...在htmlUnit中執行Javascript
任何想法我怎麼能做到這一點...
這裏的示例代碼解釋什麼是我真正意圖做...
public static void main(String[] args) {
final WebClient webClient = new WebClient();
HtmlPage page;
try {
page = webClient
.getPage("http://www.somepage.com");
System.out.println(page.asXml());
System.out.println(page.getByXPath("//script"));
BufferedInputStream buffer = null;
// System.out.print("getWebSite " + urlValue + "\n");
URL url = new URL(
"http://www.somepage.com/someJS.js");
HttpURLConnection urlc = (HttpURLConnection) url.openConnection();
buffer = new BufferedInputStream(urlc.getInputStream());
StringBuilder builder = new StringBuilder();
int byteRead;
while ((byteRead = buffer.read()) != -1)
builder.append((char) byteRead);
buffer.close();
ScriptResult result = page.executeJavaScript(builder.toString());
Object jsResult = result.getJavaScriptResult();
HtmlPage afterExecution = (HtmlPage) result.getNewPage();
System.out.println(afterExecution.asXml());
} catch (FailingHttpStatusCodeException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
是htmlUnit的問題還是比較? –