Question

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?

Was it helpful?

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)

OTHER TIPS

The setThrowExceptionOnFailingStatusCode(boolean) is defined on the both the class webClient and WebClientOptions.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top