当我执行这个简单的代码以将网站内容作为文本时,它会显示我无法理解的错误。
import java.io.IOException;
import java.net.MalformedURLException;
import com.gargoylesoftware.htmlunit.FailingHttpStatusCodeException;
import com.gargoylesoftware.htmlunit.ScriptException;
import com.gargoylesoftware.htmlunit.WebClient;
import com.gargoylesoftware.htmlunit.html.HtmlPage;
public class sd {
public static void main(String[] args) {
sd vip=new sd();
try {
vip.homePage();
} catch (Exception e) {
e.printStackTrace();
}
System.out.print("sssss");
}
public void homePage() throws Exception, ScriptException {
final WebClient webClient = new WebClient();
final HtmlPage page =
(HtmlPage)webClient.getPage("http://timesofindia.indiatimes.com/");
String pageAsText = page.asText();
String pageAsXML = page.asXml();
// System.out.println(pageAsXML);
System.out.println("////////////////////output//////////////////////////");
System.out.println(pageAsText);
// System.out.println(pageAsXML);
System.out.println("////////////////////output ends//////////////////////////");
}
}
我得到的错误:
======= EXCEPTION START ======== Exception class=[com.gargoylesoftware.htmlunit.ScriptException] com.gargoylesoftware.htmlunit.ScriptException: Exception invoking jsxFunction_write at com.gargoylesoftware.htmlunit.javascript.JavaScriptEngine$HtmlUnitContextAction.run(JavaScriptEngine.java:595) Caused by: java.lang.RuntimeException: Exception invoking jsxFunction_write Caused by: com.gargoylesoftware.htmlunit.ScriptException: Exception invoking jsxFunction_write at com.gargoylesoftware.htmlunit.javascript.JavaScriptEngine$HtmlUnitContextAction.run(JavaScriptEngine.java:595)
答案 0 :(得分:22)
自HtmlUnit版本2.11以来,不推荐使用WebClient::setThrowExceptionOnScriptError
方法。在较新版本中使用以下内容:
webClient.getOptions().setThrowExceptionOnScriptError(false);
答案 1 :(得分:5)
将您的webClient设置为不抛出javascript异常
webClient.setThrowExceptionOnScriptError(假);
如果不是enougth,请在初始化webclient时将FF设置为客户端行为。
webClient = new WebClient(BrowserVersion.FIREFOX_3_6); webClient = new WebClient(BrowserVersion.FIREFOX_10); //取决于HtmlUnit版本
答案 2 :(得分:0)