12

私は HTMLUnit を使用しています。それは私の要件によく合っています。しかし、それは非常に遅いようです。例:HTMLUnitを使用して次のシナリオを自動化しました

Goto Google page
Enter some text
Click on the search button
Get the title of the results page
Click on the first result.

コード :

long t1=System.currentTimeMillis();
Logger logger=Logger.getLogger("");
logger.setLevel(Level.OFF);
WebClient webClient=createWebClient();
WebRequest webReq=new WebRequest(new URL("http://google.lk"));

HtmlPage googleMainPage=webClient.getPage(webReq);
HtmlTextInput searchTextField=(HtmlTextInput) googleMainPage.getByXPath("//input[@name='q']").get(0);
HtmlButton searchButton=(HtmlButton) googleMainPage.getByXPath("//button[@name='btnK']").get(0);

searchTextField.type("Sri Lanka");
System.out.println("Text typed!");
HtmlPage googleResultsPage= searchButton.click();
System.out.println("Search button clicked!");

System.out.println("Title : " + googleResultsPage.getTitleText());

HtmlAnchor firstResultLink=(HtmlAnchor) googleResultsPage.getByXPath("//a[@class='l']").get(0);
HtmlPage firstResultPage=firstResultLink.click();
System.out.println("First result clicked!");

System.out.println("Title : " + firstResultPage.getTitleText());
//System.out.println(firstResultPage.asText());
long t2=System.currentTimeMillis();
long diff=t2-t1;
System.out.println("Time elapsed : "  + milliSecondsToHrsMinutesAndSeconds(diff));

webClient.closeAllWindows();

それは100%うまく機能します。でも3分41秒かかる

実行が遅い理由は、ページ上のすべての要素を検証していると思います。

私の質問は、 HTMLUnit の実行時間を短縮する方法ですか? Web ページの検証を無効にする方法はありますか。

前もって感謝します!

4

3 に答える 3

14

現在の htmlUnit 2.13 では、設定オプションは maxmax が提供するものとは少し異なります。

final WebClient webClient = new WebClient(BrowserVersion.CHROME);
webClient.getOptions().setCssEnabled(false);//if you don't need css
webClient.getOptions().setJavaScriptEnabled(false);//if you don't need js
HtmlPage page = webClient.getPage("http://XXX.xxx.xx");
...

私自身のテストでは、これはデフォルトのオプションよりも 8 倍高速です (これは Web ページに依存する可能性があることに注意してください)。

于 2013-11-08T04:55:21.003 に答える
9
  • 必ず最新の htmlunit バージョン (2.9) を使用してください。以前のバージョンからパフォーマンスが向上しました。

設定したオプションに応じて、20 秒または 40 秒以内にあなたの例を完成させます。webClient の初期化が表示されないので、問題である可能性があると思います。

20代の治療の初期化は次のとおりです。

WebClient client = new WebClient(BrowserVersion.FIREFOX_3_6);
    client.setTimeout(60000);
    client.setRedirectEnabled(true);
    client.setJavaScriptEnabled(true);
    client.setThrowExceptionOnFailingStatusCode(false);
    client.setThrowExceptionOnScriptError(false);
    client.setCssEnabled(false);
    client.setUseInsecureSSL(true);
于 2012-05-04T07:56:05.717 に答える
1

また、javascriptに時間制限を設定することをお勧めします。

   client.setJavaScriptTimeout(30000); //e.g. 30s
于 2013-01-08T09:58:35.313 に答える