title: Java – How can I Log into a Website with HtmlUnit?
body: <p>I am writing a Java program to log into the website my school uses to post grades.</p>
  
  <p>This is the url of the login form: <a href="https://ma-andover.myfollett.com/aspen/logon.do" rel="nofollow noreferrer">https://ma-andover.myfollett.com/aspen/logon.do</a></p>
  
  <p>This is the HTML of the login form:</p>
  
  <pre><code>&lt;form name="logonForm" method="post" action="/aspen/logon.do" autocomplete="off"&gt;&lt;div&gt;&lt;input type="hidden" name="org.apache.struts.taglib.html.TOKEN" value="30883f4c7e25a014d0446b5251aebd9a"&gt;&lt;/div&gt;
  &lt;input type="hidden" id="userEvent" name="userEvent" value="930"&gt;
  &lt;input type="hidden" id="userParam" name="userParam" value=""&gt;
  &lt;input type="hidden" id="operationId" name="operationId" value=""&gt;
  &lt;input type="hidden" id="deploymentId" name="deploymentId" value="ma-andover"&gt;
  &lt;input type="hidden" id="scrollX" name="scrollX" value="0"&gt;
  &lt;input type="hidden" id="scrollY" name="scrollY" value="0"&gt;
  &lt;input type="hidden" id="formFocusField" name="formFocusField" value="username"&gt;
  &lt;input type="hidden" name="mobile" value="false"&gt;
  &lt;input type="hidden" name="SSOLoginDone" value=""&gt;
  &lt;center&gt;
  &lt;img src="images/spacer.gif" height="15" width="1"&gt;
  
  &lt;script language="JavaScript"&gt;
  document.forms[0].elements['deploymentId'].value = 'ma-andover';
  &lt;/script&gt;
  
  &lt;script language="JavaScript"&gt;
  $(function()
  {
  $('form').attr('autocomplete', 'off');
  var name = $('#username');
  var password = $('#password');
  name.attr('autocomplete', 'off');
  password.attr('autocomplete', 'off');
  if (name.val() == '')
  {
  password.attr('disabled','disabled');
  }
  });
  &lt;/script&gt;
  
  &lt;img src="images/spacer.gif" height="30" width="1"&gt;
  &lt;table border="0" cellpadding="0" cellspacing="0"&gt;
  &lt;tbody&gt;&lt;tr&gt;
  &lt;td&gt;
  &lt;div id="logonDetailContainer" class="logonDetailContainer"&gt;
  &lt;table border="0" cellpadding="0" cellspacing="0"&gt;
  
  &lt;tbody&gt;&lt;tr&gt;
  &lt;td&gt;
  &lt;label style="text-align: center; margin-bottom: 0px"&gt;Andover Public Schools&lt;/label&gt;
  &lt;img src="images/spacer.gif" height="10" width="1"&gt;
  &lt;hr class="logonHorizontalRule"&gt;
  &lt;/td&gt;
  &lt;/tr&gt;
  
  &lt;tr&gt;
  &lt;td&gt;
  &lt;img src="images/spacer.gif" height="10" width="1"&gt;
  
  
  &lt;input type="text" name="fakeuser" style="display: none"&gt;
  &lt;input type="password" name="fakepassword" style="display: none"&gt;
  
  &lt;/td&gt;
  &lt;/tr&gt;
  &lt;tr&gt;
  &lt;td class="labelCell"&gt;
  
  &lt;label&gt;Login ID&lt;/label&gt;
  &lt;input type="text" name="username" tabindex="1" value="" onkeypress="$('#password').prop('disabled', false)" id="username" class="logonInput" autocomplete="off"&gt;
  
  &amp;nbsp;
  
  &lt;/td&gt;
  &lt;/tr&gt;
  &lt;tr&gt;
  &lt;td class="labelCell"&gt;
  
  &lt;label&gt;Password&lt;/label&gt;
  &lt;input id="password" type="password" name="password" tabindex="2" value="" class="logonInput" autocomplete="off" disabled="disabled"&gt;
  
  &lt;a href="javascript:EmbeddedPopup.popupManager.open('passwordRecovery.do?isSecondary=false&amp;amp;deploymentId=ma-andover', 400, 400, 100)" tabindex="5" style="float: right"&gt;
  I forgot my password
  &lt;/a&gt;
  
  
  &lt;/td&gt;
  &lt;/tr&gt;
  &lt;tr&gt;
  &lt;td width="1" class="logonTopPadding" style="float: left"&gt;
  &lt;input type="submit" tabindex="3" value="Log On" class="log-button"&gt;
  &lt;/td&gt;
  &lt;/tr&gt;
  
  &lt;/tbody&gt;&lt;/table&gt;
  &lt;/div&gt;
  &lt;/td&gt;
  &lt;/tr&gt;
  &lt;/tbody&gt;&lt;/table&gt;
  
  &lt;/center&gt;
  &lt;script&gt;
  setTimeout(function(){window.location.reload(true);}, 1800000);
  &lt;/script&gt;
  &lt;/form&gt;
  </code></pre>
  
  <p>I am trying to use the following code to log in:</p>
  
  <pre><code>import com.gargoylesoftware.htmlunit.WebClient;
  import com.gargoylesoftware.htmlunit.html.HtmlForm;
  import com.gargoylesoftware.htmlunit.html.HtmlPage;
  
  public class LoginAttempt {
  
      public static void main(String[] args) throws Exception {  
              WebClient webClient = new WebClient();
  
              HtmlPage page = (HtmlPage) webClient.getPage("https://ma-andover.myfollett.com/aspen/logon.do"); 
              HtmlForm form = page.getFormByName("logonForm"); 
              form.getInputByName("username").setValueAttribute("myUsername"); //works fine 
              form.getInputByName("password").setValueAttribute("myPassword"); //does not work 
  
              page = form.getInputByValue("Log On").click(); //works fine
  
              System.out.println(page.asText());
      } 
  
  }
  </code></pre>
  
  <p>The program fills the username box and clicks the "Log On" button, but it does not fill the password box.  What can I change to make this program work?  I suspect the "type = 'password'" attribute of the password box has something to do with the problem, but please correct me if I am wrong. Any help is appreciated.  Thank you very much.</p>
  
  <p>The target page: <a href="https://ma-andover.myfollett.com/aspen/home.do" rel="nofollow noreferrer">https://ma-andover.myfollett.com/aspen/home.do</a></p>
  
  <p>And this is my output, in case it might be helpful: </p>
  
  <pre><code>Aspen: Log On
  
  Aspen
  
      About Aspen
  Andover Public Schools
  Login ID myUsername  
  Password I forgot my password
  Log On
  
  Copyright © 2003-2014 Follett School Solutions. All rights reserved.
  Follett Corporation Follett Software Company Aspen Terms of Use
  
  You must enter a password.
  OK
  </code></pre>