title: Java HtmlUnit login with user and password returns to login page
body: <p>I am trying to perform a login using HtmlUnit with valid username and password. After a successfull login, I want to perform a few operations on the resulting page. The problem is that I cannot get passed the login stage - I keep getting it the same page. Strangely enough, I recorded a successfull login by HtmlUnit Scripter Firefox plugin and replayed the resulting code afterwards - same problem. Any ideas?</p>
  
  <p>Here is the code:</p>
  
  <pre><code>&lt;pre&gt;
  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;
  &lt;code&gt;
  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("yacov.schondorf@gmail.com");
  
                 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();
            }
       }
  
  }
  </code></pre>