HTMLunit抑制错误:已弃用?

时间:2013-12-27 14:45:49

标签: java error-handling htmlunit

我正在尝试抑制HTMLunit在加载页面时几乎总是显示的JavaScript错误。

但奇怪的是,以下代码不起作用:

import com.gargoylesoftware.htmlunit.WebClient;
import com.gargoylesoftware.htmlunit.html.HtmlForm;
import com.gargoylesoftware.htmlunit.html.HtmlPage;
import com.gargoylesoftware.htmlunit.html.HtmlPasswordInput;
import com.gargoylesoftware.htmlunit.html.HtmlSubmitInput;
import com.gargoylesoftware.htmlunit.html.HtmlTextInput;

public class HttpClientLogin {

    public static void main(String[] args) throws Exception
    {
        HttpClientLogin logInNow = new HttpClientLogin();

        logInNow.loadPage();
    }

    public void loadPage() throws Exception {

        WebClient webClient = new WebClient();

        HtmlPage currentPage = webClient.getPage("the url link here");

            webClient.setThrowExceptionOnFailingStatusCode(false);

        String textSource = currentPage.asText();
        String xmlSource = currentPage.asXml();

        System.out.println(xmlSource);
    }
}

它出现以下错误:

The method setThrowExceptionOnFailingStatusCode(boolean) is undefined for the type WebClient

这些方法是否已被弃用,或者我使用了错误的包?

2 个答案:

答案 0 :(得分:5)

setThrowExceptionOnFailingStatusCode(boolean)WebClientOptions课程中定义,而不在WebClient中定义。

webClient.getOptions().setThrowExceptionOnFailingStatusCode(false);

http://htmlunit.sourceforge.net/apidocs/com/gargoylesoftware/htmlunit/WebClientOptions.html#setThrowExceptionOnFailingStatusCode(boolean)

答案 1 :(得分:0)

setThrowExceptionOnFailingStatusCode(boolean)在类webClient和WebClientOptions上定义。