我一直在使用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的执行时间?有什么方法可以禁用网页上的验证。
提前致谢!
对于当前的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倍。(请注意,这可能取决于网页)