java htmlunit在点击页面的提交按钮时抛出ScriptException

时间:2018-05-31 03:45:11

标签: java htmlunit

 public HtmlPage getCBSE18(String roll, String schCode, String centerno) throws IOException {
        HtmlPage page = webClient.getPage(CBSE18);

        final HtmlForm form = page.getFormByName("FrontPage_Form1");
        //System.out.println(form.asText());
        final HtmlTextInput regField = form.getInputByName("regno");
        regField.type(roll);

        final HtmlTextInput schField = form.getInputByName("sch");
        schField.type(schCode);

        final HtmlTextInput centerField = form.getInputByName("cno");
        centerField.type(schCode);

        final HtmlSubmitInput button = form.getInputByName("B2");
        webClient.waitForBackgroundJavaScript(1000);
        System.out.println(button.asText());

        final HtmlPage page2 = button.click();

        System.out.println(page2.asText());
        return page2;

以上功能从“http://www.cbseresults.nic.in/class12zpq/class12th18.htm”加载一个页面,这是一个学生可以看到他的CBSE 2018考试成绩的网站....并填写必填字段,之后点击提交按钮在页面上..然后它返回显示候选人结果的页面......但我得到的只是这个例外......

    May 31, 2018 8:55:44 AM com.gargoylesoftware.htmlunit.IncorrectnessListenerImpl notify
WARNING: Obsolete content type encountered: 'application/x-javascript'.
Submit
Exception in thread "main" ======= EXCEPTION START ========
EcmaError: lineNumber=[522] column=[0] lineSource=[null] name=[TypeError] sourceName=[http://www.cbseresults.nic.in/class12zpq/cal2.js] message=[TypeError: Cannot find function routeEvent in object [object HTMLDocument]. (http://www.cbseresults.nic.in/class12zpq/cal2.js#522)]
com.gargoylesoftware.htmlunit.ScriptException: TypeError: Cannot find function routeEvent in object [object HTMLDocument]. (http://www.cbseresults.nic.in/class12zpq/cal2.js#522)
    at com.gargoylesoftware.htmlunit.javascript.JavaScriptEngine$HtmlUnitContextAction.run(JavaScriptEngine.java:914)
    at net.sourceforge.htmlunit.corejs.javascript.Context.call(Context.java:599)
    at net.sourceforge.htmlunit.corejs.javascript.ContextFactory.call(ContextFactory.java:527)
    at com.gargoylesoftware.htmlunit.javascript.JavaScriptEngine.callFunction(JavaScriptEngine.java:843)
    at com.gargoylesoftware.htmlunit.javascript.JavaScriptEngine.callFunction(JavaScriptEngine.java:815)
    at com.gargoylesoftware.htmlunit.html.HtmlPage.executeJavaScriptFunction(HtmlPage.java:2520)
    at com.gargoylesoftware.htmlunit.html.HtmlPage.executeJavaScriptFunction(HtmlPage.java:2513)
    at com.gargoylesoftware.htmlunit.javascript.host.event.EventListenersContainer.executeEventHandler(EventListenersContainer.java:308)
    at com.gargoylesoftware.htmlunit.javascript.host.event.EventListenersContainer.executeBubblingListeners(EventListenersContainer.java:328)
    at com.gargoylesoftware.htmlunit.javascript.host.event.EventTarget.fireEvent(EventTarget.java:192)
    at com.gargoylesoftware.htmlunit.html.DomElement$2.run(DomElement.java:1420)
    at net.sourceforge.htmlunit.corejs.javascript.Context.call(Context.java:599)
    at net.sourceforge.htmlunit.corejs.javascript.ContextFactory.call(ContextFactory.java:527)
    at com.gargoylesoftware.htmlunit.html.DomElement.fireEvent(DomElement.java:1425)
    at com.gargoylesoftware.htmlunit.html.DomElement.doClickFireClickEvent(DomElement.java:1092)
    at com.gargoylesoftware.htmlunit.html.DomElement.click(DomElement.java:1023)
    at com.gargoylesoftware.htmlunit.html.DomElement.click(DomElement.java:973)
    at com.gargoylesoftware.htmlunit.html.DomElement.click(DomElement.java:903)
    at com.gargoylesoftware.htmlunit.html.DomElement.click(DomElement.java:884)
    at com.gargoylesoftware.htmlunit.html.DomElement.click(DomElement.java:865)
    at resultrobot.html.CBSEResultFetcher.getCBSE18(CBSEResultFetcher.java:89)
    at resultrobot.html.CBSEResultFetcher.main(CBSEResultFetcher.java:128)
Caused by: net.sourceforge.htmlunit.corejs.javascript.EcmaError: TypeError: Cannot find function routeEvent in object [object HTMLDocument]. (http://www.cbseresults.nic.in/class12zpq/cal2.js#522)
    at net.sourceforge.htmlunit.corejs.javascript.ScriptRuntime.constructError(ScriptRuntime.java:4130)
    at net.sourceforge.htmlunit.corejs.javascript.ScriptRuntime.constructError(ScriptRuntime.java:4108)
    at net.sourceforge.htmlunit.corejs.javascript.ScriptRuntime.typeError(ScriptRuntime.java:4141)
    at net.sourceforge.htmlunit.corejs.javascript.ScriptRuntime.typeError2(ScriptRuntime.java:4160)
    at net.sourceforge.htmlunit.corejs.javascript.ScriptRuntime.notFunctionError(ScriptRuntime.java:4236)
    at net.sourceforge.htmlunit.corejs.javascript.ScriptRuntime.getPropFunctionAndThisHelper(ScriptRuntime.java:2520)
    at net.sourceforge.htmlunit.corejs.javascript.ScriptRuntime.getPropFunctionAndThis(ScriptRuntime.java:2502)
    at net.sourceforge.htmlunit.corejs.javascript.Interpreter.interpretLoop(Interpreter.java:1327)
    at net.sourceforge.htmlunit.corejs.javascript.Interpreter.interpret(Interpreter.java:815)
    at net.sourceforge.htmlunit.corejs.javascript.InterpretedFunction.call(InterpretedFunction.java:111)
    at net.sourceforge.htmlunit.corejs.javascript.ContextFactory.doTopCall(ContextFactory.java:417)
    at com.gargoylesoftware.htmlunit.javascript.HtmlUnitContextFactory.doTopCall(HtmlUnitContextFactory.java:325)
    at net.sourceforge.htmlunit.corejs.javascript.ScriptRuntime.doTopCall(ScriptRuntime.java:3424)
    at com.gargoylesoftware.htmlunit.javascript.JavaScriptEngine$4.doRun(JavaScriptEngine.java:836)
    at com.gargoylesoftware.htmlunit.javascript.JavaScriptEngine$HtmlUnitContextAction.run(JavaScriptEngine.java:899)
    ... 21 more
Enclosed exception: 
net.sourceforge.htmlunit.corejs.javascript.EcmaError: TypeError: Cannot find function routeEvent in object [object HTMLDocument]. (http://www.cbseresults.nic.in/class12zpq/cal2.js#522)
    at net.sourceforge.htmlunit.corejs.javascript.ScriptRuntime.constructError(ScriptRuntime.java:4130)
    at net.sourceforge.htmlunit.corejs.javascript.ScriptRuntime.constructError(ScriptRuntime.java:4108)
    at net.sourceforge.htmlunit.corejs.javascript.ScriptRuntime.typeError(ScriptRuntime.java:4141)
    at net.sourceforge.htmlunit.corejs.javascript.ScriptRuntime.typeError2(ScriptRuntime.java:4160)
    at net.sourceforge.htmlunit.corejs.javascript.ScriptRuntime.notFunctionError(ScriptRuntime.java:4236)
    at net.sourceforge.htmlunit.corejs.javascript.ScriptRuntime.getPropFunctionAndThisHelper(ScriptRuntime.java:2520)
    at net.sourceforge.htmlunit.corejs.javascript.ScriptRuntime.getPropFunctionAndThis(ScriptRuntime.java:2502)
    at net.sourceforge.htmlunit.corejs.javascript.Interpreter.interpretLoop(Interpreter.java:1327)
    at script.markClick(http://www.cbseresults.nic.in/class12zpq/cal2.js:522)
    at net.sourceforge.htmlunit.corejs.javascript.Interpreter.interpret(Interpreter.java:815)
    at net.sourceforge.htmlunit.corejs.javascript.InterpretedFunction.call(InterpretedFunction.java:111)
    at net.sourceforge.htmlunit.corejs.javascript.ContextFactory.doTopCall(ContextFactory.java:417)
    at com.gargoylesoftware.htmlunit.javascript.HtmlUnitContextFactory.doTopCall(HtmlUnitContextFactory.java:325)
    at net.sourceforge.htmlunit.corejs.javascript.ScriptRuntime.doTopCall(ScriptRuntime.java:3424)
    at com.gargoylesoftware.htmlunit.javascript.JavaScriptEngine$4.doRun(JavaScriptEngine.java:836)
    at com.gargoylesoftware.htmlunit.javascript.JavaScriptEngine$HtmlUnitContextAction.run(JavaScriptEngine.java:899)
    at net.sourceforge.htmlunit.corejs.javascript.Context.call(Context.java:599)
    at net.sourceforge.htmlunit.corejs.javascript.ContextFactory.call(ContextFactory.java:527)
    at com.gargoylesoftware.htmlunit.javascript.JavaScriptEngine.callFunction(JavaScriptEngine.java:843)
    at com.gargoylesoftware.htmlunit.javascript.JavaScriptEngine.callFunction(JavaScriptEngine.java:815)
    at com.gargoylesoftware.htmlunit.html.HtmlPage.executeJavaScriptFunction(HtmlPage.java:2520)
    at com.gargoylesoftware.htmlunit.html.HtmlPage.executeJavaScriptFunction(HtmlPage.java:2513)
    at com.gargoylesoftware.htmlunit.javascript.host.event.EventListenersContainer.executeEventHandler(EventListenersContainer.java:308)
    at com.gargoylesoftware.htmlunit.javascript.host.event.EventListenersContainer.executeBubblingListeners(EventListenersContainer.java:328)
    at com.gargoylesoftware.htmlunit.javascript.host.event.EventTarget.fireEvent(EventTarget.java:192)
    at com.gargoylesoftware.htmlunit.html.DomElement$2.run(DomElement.java:1420)
    at net.sourceforge.htmlunit.corejs.javascript.Context.call(Context.java:599)
    at net.sourceforge.htmlunit.corejs.javascript.ContextFactory.call(ContextFactory.java:527)
    at com.gargoylesoftware.htmlunit.html.DomElement.fireEvent(DomElement.java:1425)
    at com.gargoylesoftware.htmlunit.html.DomElement.doClickFireClickEvent(DomElement.java:1092)
    at com.gargoylesoftware.htmlunit.html.DomElement.click(DomElement.java:1023)
    at com.gargoylesoftware.htmlunit.html.DomElement.click(DomElement.java:973)
    at com.gargoylesoftware.htmlunit.html.DomElement.click(DomElement.java:903)
    at com.gargoylesoftware.htmlunit.html.DomElement.click(DomElement.java:884)
    at com.gargoylesoftware.htmlunit.html.DomElement.click(DomElement.java:865)
    at resultrobot.html.CBSEResultFetcher.getCBSE18(CBSEResultFetcher.java:89)
    at resultrobot.html.CBSEResultFetcher.main(CBSEResultFetcher.java:128)
== CALLING JAVASCRIPT ==
  function markClick(e) {
      if (isIE || isOpera6) {
          winX = event.screenX;
          winY = event.screenY;
      } else {
          if (isN4 || isN6) {
              winX = e.screenX;
              winY = e.screenY;
              document.routeEvent(e);
          }
      }
      if (isN4 || isN6) {
          document.routeEvent(e);
      } else {
          event.cancelBubble = false;
      }
      return true;
  }
======= EXCEPTION END ========
Java Result: 1

最后它起作用了         webClient.getOptions()setJavaScriptEnabled(假)。         。webClient.getOptions()setThrowExceptionOnScriptError(假); 它的javascript抛出错误

1 个答案:

答案 0 :(得分:0)

第一次,通过启用选项

告诉webclient不要为脚本错误抛出异常
webClient.getOptions().setThrowExceptionOnScriptError(false);