2016-11-17 88 views
1

我正在用html單元解析網站。這個過程基本上;HtmlUnit單擊後沒有獲得內容

WebClient client = new WebClient(BrowserVersion.CHROME); 
client.waitForBackgroundJavaScript(5 * 1000); 
HtmlPage page = client.getPage("http://www.exapmle.com"); //here it waits to run js code. 

HtmlUnorderedList ul = (HtmlUnorderedList) page.getByXPath("//ul[contains(@class, 'class-name')]").get(0); 
HtmlListItem li = (HtmlListItem) ul.getChildNodes().get(1); // I want to click li and get result page. But it takes a little time to execute. 

li.click(); 

client.waitForBackgroundJavaScript(5 * 1000); //At here it does not do what I want. 

之後,當我檢查頁面時,我發現它的內容沒有改變。

我能做些什麼來獲得正確的頁面結果?

感謝。

回答

0

你可以嘗試輪詢一個javascript條件爲真

int attempts = 20; 
int pollMillis = 500; 
boolean success = false; 
for (int i = 0; i < attempts && !success; i++) { 
    TimeUnit.MILLISECONDS.sleep(pollMillis); 
    if (someJavascriptCondition == true) { 
     success = true; 
    } 
} 
if (!success) throw new RuntimeException(String.format("Condition not met after %s millis", attempts * pollMillis); 

類似的技術討論here

+0

我沒有這樣的Java腳本條件:/ – xxlali

+0

當然你可以。檢查一個微調圖像已停止或div已更新等等 –

0
WebClient client = new WebClient; 
HtmlPage page = client.getPage("http://www.exapmle.com"); 
client.waitForBackgroundJavaScript(5 * 1000); 
Thread.sleep(10*1000);// this code will waite to 10 seconds 
HtmlUnorderedList ul = (HtmlUnorderedList) page.getByXPath("//ul[contains(@class, 'class-name')]").get(0); 
HtmlListItem li = (HtmlListItem) ul.getChildNodes().get(1); // I want to click li and get result page. But it takes a little time to execute. 

li.click(); 

client.waitForBackgroundJavaScript(5 * 1000); 
// this code will waite to 10 seconds 
Thread.sleep(10*1000); 

使用Thread.sleep()方法,而不是waitForBackgroundJavaScript 對我的作品!

+0

不,它不起作用:/ – xxlali

0

您可以使用JavaScriptJobManager來檢查尚未完成的JavaScript作業的數量。撥打click()後,請嘗試以下代碼。

JavaScriptJobManager manager = page.getEnclosingWindow().getJobManager(); 
while (manager.getJobCount() > 0) { 
    System.out.printlin("Jobs remaining: " + manager.getJobCount()); 
    Thread.sleep(1000); 
} 

您可能想要添加另一種方式來結束while循環,以防JavaScript作業永遠無法完成。就個人而言,我開始手動終止工作:

JavaScriptJob job = manager.getEarliestJob(); 
System.out.println("Stopping job: " + job.getId()); 
manager.stopJob(job.getId()); 

希望這有助於。