2013-09-01 214 views
4

我試圖在www.meetme.com上發送消息,但無法弄清楚如何執行此操作。我可以在評論區域輸入消息,但點擊發送按鈕不會執行任何操作。我究竟做錯了什麼?當我登錄並按登錄按鈕頁面確實會改變,一切都很好。任何人有任何想法或線索?HtmlUnit按鈕單擊

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("[email protected]"); 
     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(); 
     } 

回答

3

不知道你訪問的網頁太多,你只是不能執行禁用JavaScript的AJAX請求。如果更改不會導致成功,那麼您將不得不繼續調試,但要確保啓用了JavaScript。

此外,請確保您使用HtmlUnit 1.12並更新代碼中的所有不推薦使用的方法。

順便說一句,我也建議關閉JavaScript警告。查看this answer,看看你如何做到這一點。

+1

@大部分它的工作!非常感謝! – altoids

+0

歡迎:)只需檢查答案中的鏈接以刪除可怕的輸出。 (我只是不得不更新鏈接,因爲答案有點過時) –