2013-07-24 328 views
2

我想用有效的用戶名和密碼使用HtmlUnit執行登錄。成功登錄後,我想在結果頁面上執行一些操作。問題是我無法通過登錄階段 - 我一直在同一頁面。奇怪的是,我記錄了HtmlUnit Scripter Firefox插件的成功登錄,然後重播了結果代碼 - 同樣的問題。有任何想法嗎?Java HtmlUnit用戶名和密碼登錄返回登錄頁

下面是代碼:

<pre> 
import com.gargoylesoftware.htmlunit.BrowserVersion; 
import com.gargoylesoftware.htmlunit.FailingHttpStatusCodeException; 
import com.gargoylesoftware.htmlunit.WebClient; 
import com.gargoylesoftware.htmlunit.html.HtmlElement; 
import com.gargoylesoftware.htmlunit.html.HtmlPage; 
import com.gargoylesoftware.htmlunit.html.HtmlPasswordInput; 
import com.gargoylesoftware.htmlunit.html.HtmlTextInput; 

import java.io.IOException; 
import java.net.MalformedURLException; 
<code> 
public class LoginSimulation 
{ 
    public static void main(String args[]) 
    { 
      HtmlPage page = null; 
      String url = "http://www.ravmilim.co.il/naerr.asp"; 

      WebClient webClient = new WebClient(BrowserVersion.FIREFOX_3_6); 
      webClient.setThrowExceptionOnScriptError(false); 

      try 
      { 
       page = webClient.getPage(url); 

       HtmlTextInput userInput = (HtmlTextInput) page.getElementById("txtUser"); 
       userInput.setValueAttribute("[email protected]"); 

       HtmlPasswordInput passwordInput = (HtmlPasswordInput) page.getElementById("txtPass"); 
       passwordInput.setValueAttribute("5750201"); 

       HtmlElement theElement2 = (HtmlElement) page.getElementById("submitButton"); 
       page = theElement2.click(); // the login does not work. Login page is displayed again 

       System.out.println(page.asText()); 
       webClient.closeAllWindows();    
      } 
      catch (FailingHttpStatusCodeException e1) 
      { 
       System.out.println("FailingHttpStatusCodeException thrown:" + e1.getMessage()); 
       e1.printStackTrace(); 
      } 
      catch (MalformedURLException e1) 
      { 
       System.out.println("MalformedURLException thrown:" + e1.getMessage()); 
       e1.printStackTrace(); 
      } 
      catch (IOException e1) 
      { 
       System.out.println("IOException thrown:" + e1.getMessage()); 
       e1.printStackTrace(); 
      } 
      catch(Exception e) 
      { 
       System.out.println("General exception thrown:" + e.getMessage()); 
       e.printStackTrace(); 
      } 
    } 

} 

回答

0

看起來你的代碼就可以了。我已經手動嘗試過,有時會記錄,有時候不會。問題出在登錄操作後出現的彈出窗口中。 你可以看到它從頁面上的JavaScript:

if ($("#ErrorDuplicationLogin").text() != '') 
    {  
    $(".PopUpContent").show(); 
    $(".pagePopBackground").show();      

    } 

}); 

所以嘗試登錄和註銷手動,之後啓動程序。另外不要忘記使用HtmlUnit註銷。謝謝。

+0

你是對的!我註銷了其他客戶端,並等待先前的htmlunit登錄過期,然後我才能夠登錄!雖然我的問題還沒有結束。在下一頁中,我試圖設置搜索框的值,單擊搜索按鈕並解析結果。問題是結果顯示在另一個框架中,我無法獲得其他框架。這裏是修改後的代碼: – user2045777

+0

System.out.println(page.asText())之後 - 我添加了: – user2045777

+0

HtmlPage framePage =(HtmlPage)nextPage.getFrames()。get(0).getEnclosedPage(); HtmlTextInput searchBox =(HtmlTextInput)framePage.getForms()。get(0).getInputsByName(「searchBox」)。get(0); searchBox.setValueAttribute(「word」); HtmlAnchor anchor = framePage.getHtmlElementById(「sl」); HtmlPage page1 =(HtmlPage)anchor.click(); try { HtmlPage resultsPage =(HtmlPage)page1.getFrameByName(「resault1」)。getEnclosedPage(); (例外e){ e.printStackTrace(); } framePage.getAnchorByHref(「logout.asp」)。click(); – user2045777