HTMLUnit:执行速度超慢?

编程入门 行业动态 更新时间:2024-10-21 06:18:31
本文介绍了HTMLUnit:执行速度超慢?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我一直在使用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:执行速度超慢?

本文发布于:2023-11-29 00:43:11,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1644611.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:速度   HTMLUnit

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!