gpt4 book ai didi

java - HTMLUnit : super slow execution?

转载 作者:搜寻专家 更新时间:2023-10-30 21:19:02 25 4
gpt4 key购买 nike

我一直在使用 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 倍。(请注意,这可能取决于网页)

关于java - HTMLUnit : super slow execution?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10442803/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com