weixin_33743880 2015-10-19 15:42 采纳率: 0%
浏览 41

HTMLUnit Ajax失败

I am using HtmlUnit and trying to get page information. URL I am scraping is: http://www.mpcz.co.in/portal/Bhopal_home.portal?_nfpb=true&_pageLabel=custCentre_viewBill_bpl 'submitButton' click calls a ajax which fails. I am not able to find the root cause of the ajax failure. I debugged it using Visual Debugger.

Input data: IVRS, 23046091095250, Bill Desk Payment

Below is code.

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条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 随身WiFi网络灯亮但是没有网络,如何解决?
    • ¥15 gdf格式的脑电数据如何处理matlab
    • ¥20 重新写的代码替换了之后运行hbuliderx就这样了
    • ¥100 监控抖音用户作品更新可以微信公众号提醒
    • ¥15 UE5 如何可以不渲染HDRIBackdrop背景
    • ¥70 2048小游戏毕设项目
    • ¥20 mysql架构,按照姓名分表
    • ¥15 MATLAB实现区间[a,b]上的Gauss-Legendre积分
    • ¥15 delphi webbrowser组件网页下拉菜单自动选择问题
    • ¥15 linux驱动,linux应用,多线程