Search code examples
javaerror-handlinghtmlunit

HTMLunit suppress errors: deprecated?


I am trying to suppress the JavaScript errors that HTMLunit almost always shows when loading a page.

But strangely enough, the following code does not work:

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);
    }
}

It gives the following error:

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

Are these methods deprecated or am I using the wrong package?


Solution

  • The setThrowExceptionOnFailingStatusCode(boolean) is defined in the WebClientOptions class, not in WebClient.

    webClient.getOptions().setThrowExceptionOnFailingStatusCode(false);
    

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