我一直在使用HTMLUnit。它很适合我的要求。但它似乎非常缓慢。 例如:我使用HTMLUnit自动化了以下场景
I have been using HTMLUnit . It suits my requirements well. But it seems to be extremely slow. for example : I have automated the following scenario using 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("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秒
It works 100% well. But it takes 3 minutes,41 seconds
我想执行缓慢的原因是验证页面上的每个元素。
I guess the reason for the slow execution is validating each and every element on the page.
我的问题是如何减少HTMLUnit的执行时间?有没有办法在网页上禁用验证。
My question is how to reduce the execution time of HTMLUnit ? is there any way to disable validations on webpages.
提前致谢!
推荐答案对于当前的htmlUnit 2.13,设置选项与maxmax提供的选项略有不同:
For the current htmlUnit 2.13, setting options is slightly different from what maxmax has provided:
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("XXX.xxx.xx"); ...在我自己的测试中,这比默认选项快8倍。(请注意,这可能与网页有关)
In my own test, this is 8 times faster than the default options.(Note that this could be webpage-dependent)
更多推荐
HTMLUnit:执行速度超慢?
发布评论