HTMLUnit Ajax失败

时间:2015-10-19 15:42:39

标签: javascript ajax htmlunit

我正在使用HtmlUnit并尝试获取页面信息。 我正在抓取的网址是:http://www.mpcz.co.in/portal/Bhopal_home.portal?_nfpb=true&_pageLabel=custCentre_viewBill_bpl '提交按钮'单击调用失败的ajax。我无法找到ajax失败的根本原因。我使用Visual Debugger调试它。

输入数据:IVRS,23046091095250,Bill Desk Payment

以下是代码。

final WebClient webClient = new WebClient(BrowserVersion.FIREFOX_31);

        webClient.getOptions().setThrowExceptionOnFailingStatusCode(false);
            webClient.getOptions().setThrowExceptionOnScriptError(false);
            webClient.getOptions().setCssEnabled(false);
            webClient.getOptions().setUseInsecureSSL(true);
            CookieHandler
                    .setDefault(new CookieManager(null, CookiePolicy.ACCEPT_ALL));
            webClient.setAjaxController(new NicelyResynchronizingAjaxController());
            webClient.getOptions().setTimeout(120000);
            webClient.getOptions().setJavaScriptEnabled(true);
            webClient.waitForBackgroundJavaScript(120000);

            webClient.getOptions().setRedirectEnabled(true);

            WebClientUtils.attachVisualDebugger(webClient); 


            webClient.addWebWindowListener(new WebWindowListener() {

                public void webWindowContentChanged(WebWindowEvent event) {
                    System.out.println("WeBWindow_aaa:"+ event.getEventType());
                }

                public void webWindowClosed(WebWindowEvent event) {
                    System.out.println("WeBWindow_aaa:"+ event.getEventType());
                }

                public void webWindowOpened(WebWindowEvent event) {
                    System.out.println("WeBWindow_aaa:"+ event.getEventType());
                    // TODO Auto-generated method stub

                }
            });


            webClient.setJavaScriptErrorListener(new JavaScriptErrorListener() {

                @Override
                public void timeoutError(HtmlPage page, long arg1, long arg2) {
                    // TODO Auto-generated method stub
                    System.out.println("Erroraaa :" + arg1 );

                }

                @Override
                public void scriptException(HtmlPage arg0, ScriptException arg1) {
                    // TODO Auto-generated method stub
                    System.out.println("scriptExceptionaaa :" + arg1 );
                }

                @Override
                public void malformedScriptURL(HtmlPage arg0, String arg1,
                        MalformedURLException arg2) {
                    // TODO Auto-generated method stub
                    System.out.println("malformedScriptURLaaa :" + arg1 );

                }

                @Override
                public void loadScriptError(HtmlPage arg0, URL arg1, Exception arg2) {
                    // TODO Auto-generated method stub
                    System.out.println("loadScriptErroraaa :" + arg1 );

                }
            });

            HtmlPage page = webClient
                    .getPage("http://www.mpcz.co.in/portal/Bhopal_home.portal?_nfpb=true&_pageLabel=custCentre_viewBill_bpl");

            webClient.waitForBackgroundJavaScript(20000);

            System.out.println("Page text: " + page.asText());

            HtmlTextInput identifier1Textbox = (HtmlTextInput) page.getElementById("chooseIdentifier1");
            identifier1Textbox.setAttribute("value", "IVRS");

            HtmlTextInput consumerNoTextbox = (HtmlTextInput) page.getElementById("accntId");
            consumerNoTextbox.setAttribute("value", "23046091095250");

            HtmlTextInput gatewayTextbox = (HtmlTextInput) page.getElementById("chooseGateway1");
            gatewayTextbox.setAttribute("value", "Bill Desk Payment");

            HtmlButton submitButton = (HtmlButton) page.getElementById("ext-gen31");
            webClient.waitForBackgroundJavaScript(60000);
            page = (HtmlPage) submitButton.click(); 
            webClient.waitForBackgroundJavaScript(60000);

System.out.println("Page text: " + page.asText());
        System.out.println("Finished");
        webClient.close();

0 个答案:

没有答案