我正在尝试抑制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
这些方法是否已被弃用,或者我使用了错误的包?
答案 0 :(得分:5)
setThrowExceptionOnFailingStatusCode(boolean)
在WebClientOptions
课程中定义,而不在WebClient
中定义。
webClient.getOptions().setThrowExceptionOnFailingStatusCode(false);
答案 1 :(得分:0)
setThrowExceptionOnFailingStatusCode(boolean)在类webClient和WebClientOptions上定义。