无法使用HTTPUNIT获取网页上的链接数量?

时间:2012-01-31 06:18:55

标签: java html-parsing http-unit

我已经开始学习HTTPUNIT,并找到了一个基本的例子。

在此示例中,它将访问此site。 它将搜索包含HTTPUNIT的链接。它将打印HTTPUNIT上的链接数量。我在我的机器上尝试了这个例子,它可以工作。

        WebConversation wc = new WebConversation();
        WebRequest request = new GetMethodWebRequest( "http://www.meterware.com" );
        WebResponse response = wc.getResponse( request );
        WebLink httpunitLink = response.getFirstMatchingLink( WebLink.MATCH_CONTAINED_TEXT, "HttpUnit" );
        response = httpunitLink.click();
        System.out.println( "The HttpUnit main page contains " + response.getLinks().length + " links" );

现在我已将代码更改为

  WebConversation wc = new WebConversation();
  WebRequest request = new GetMethodWebRequest( "http://www.google.com" );
  WebResponse response = wc.getResponse( request );
  WebLink httpunitLink = response.getFirstMatchingLink( WebLink.MATCH_CONTAINED_TEXT, "News" );
  response = httpunitLink.click();
  System.out.println( "The HttpUnit main page contains " + response.getLinks().length + " links" );

现在它给出了以下错误。

ConversionError: The undefined value has no properties. (httpunit; line 4)

为什么它无法访问谷歌新闻并获得链接数量?

提前谢谢。

1 个答案:

答案 0 :(得分:2)

Google主页是通过JavaScript呈现的。

HTTPUNIT有partial support for JavaScript。如果您需要使用繁重的JavaScript测试页面,请查看Selenium

相关问题