HtmlUnit button click

杀马特。学长 韩版系。学妹 提交于 2019-11-28 04:49:08

问题


I'm trying to send a message on www.meetme.com but can't figure out how to do it. I can type in the message in the comment area but clicking the Send button doesn't do anything. What am I doing wrong? When I login and press the Login button the page does change and everything is fine. Anyone have any ideas or clues?

    HtmlPage htmlPage = null;
    HtmlElement htmlElement;
    WebClient webClient = null;
    HtmlButton htmlButton;
    HtmlForm htmlForm;
    try{

        // Create and initialize WebClient object
        webClient = new WebClient(BrowserVersion.FIREFOX_17 );
        webClient.setCssEnabled(false);
        webClient.setJavaScriptEnabled(false);
        webClient.setThrowExceptionOnFailingStatusCode(false);
        webClient.setThrowExceptionOnScriptError(false);
        webClient.getOptions().setThrowExceptionOnScriptError(false);
        webClient.getOptions().setUseInsecureSSL(true);
        webClient.getCookieManager().setCookiesEnabled(true);

        /*webClient.setRefreshHandler(new RefreshHandler() {
            public void handleRefresh(Page page, URL url, int arg) throws IOException {
                 System.out.println("handleRefresh");
            }

        });*/

         htmlPage = webClient.getPage("http://www.meetme.com");
         htmlForm = htmlPage.getFirstByXPath("//form[@action='https://ssl.meetme.com/login']");

         htmlForm.getInputByName("username").setValueAttribute("blah@gmail.com");
         htmlForm.getInputByName("password").setValueAttribute("blah");

         //Signing in
         htmlButton = htmlForm.getElementById("login_form_submit");
         htmlPage = (HtmlPage) htmlButton.click();

         htmlPage = webClient.getPage("http://www.meetme.com/member/1234567890");

         System.out.println("BEFORE CLICK");
         System.out.println(htmlPage.asText());


         //type message in text area
         HtmlTextArea commentArea = (HtmlTextArea)htmlPage.getFirstByXPath("//textarea[@id='profileQMBody']");
         commentArea.setText("Testing");        


         htmlButton = (HtmlButton) htmlPage.getHtmlElementById("profileQMSend");
         htmlPage = (HtmlPage)htmlButton.click();
         webClient.waitForBackgroundJavaScript(7000);

         //The print is exactly the same as the BEFORE CLICK print
         System.out.println("AFTER CLICK");
         System.out.println(htmlPage.asText());

      }catch(ElementNotFoundException e){
        e.printStackTrace();
      }catch(Exception e){
        e.printStackTrace();
      }

回答1:


Without knowing much about the webpage you're accessing, you just can't perform an AJAX request with JavaScript disabled. If changing that doesn't result in success, then you will have to keep debugging, but make sure JavaScript is enabled.

Additionally, make sure you're using HtmlUnit 1.12 and update all the deprecated methods in your code.

BTW, I'd also recommend to turn may JavaScript warnings off. Check this answer to see how you can do that.



来源:https://stackoverflow.com/questions/18556200/htmlunit-button-click

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!