2015-07-18 234 views
3

堆棧溢出。我正在編寫一個Java程序來登錄我的學校用來發布成績的網站。Java - 如何使用HtmlUnit登錄網站?

這是登錄表單的網址:https://ma-andover.myfollett.com/aspen/logon.do

這是登錄表單的HTML:我想用下面的代碼登錄

<form name="logonForm" method="post" action="/aspen/logon.do" autocomplete="off"><div><input type="hidden" name="org.apache.struts.taglib.html.TOKEN" value="30883f4c7e25a014d0446b5251aebd9a"></div> 
<input type="hidden" id="userEvent" name="userEvent" value="930"> 
<input type="hidden" id="userParam" name="userParam" value=""> 
<input type="hidden" id="operationId" name="operationId" value=""> 
<input type="hidden" id="deploymentId" name="deploymentId" value="ma-andover"> 
<input type="hidden" id="scrollX" name="scrollX" value="0"> 
<input type="hidden" id="scrollY" name="scrollY" value="0"> 
<input type="hidden" id="formFocusField" name="formFocusField" value="username"> 
<input type="hidden" name="mobile" value="false"> 
<input type="hidden" name="SSOLoginDone" value=""> 
<center> 
<img src="images/spacer.gif" height="15" width="1"> 

<script language="JavaScript"> 
document.forms[0].elements['deploymentId'].value = 'ma-andover'; 
</script> 

<script language="JavaScript"> 
$(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'); 
} 
}); 
</script> 

<img src="images/spacer.gif" height="30" width="1"> 
<table border="0" cellpadding="0" cellspacing="0"> 
<tbody><tr> 
<td> 
<div id="logonDetailContainer" class="logonDetailContainer"> 
<table border="0" cellpadding="0" cellspacing="0"> 

<tbody><tr> 
<td> 
<label style="text-align: center; margin-bottom: 0px">Andover Public Schools</label> 
<img src="images/spacer.gif" height="10" width="1"> 
<hr class="logonHorizontalRule"> 
</td> 
</tr> 

<tr> 
<td> 
<img src="images/spacer.gif" height="10" width="1"> 


<input type="text" name="fakeuser" style="display: none"> 
<input type="password" name="fakepassword" style="display: none"> 

</td> 
</tr> 
<tr> 
<td class="labelCell"> 

<label>Login ID</label> 
<input type="text" name="username" tabindex="1" value="" onkeypress="$('#password').prop('disabled', false)" id="username" class="logonInput" autocomplete="off"> 

&nbsp; 

</td> 
</tr> 
<tr> 
<td class="labelCell"> 

<label>Password</label> 
<input id="password" type="password" name="password" tabindex="2" value="" class="logonInput" autocomplete="off" disabled="disabled"> 

<a href="javascript:EmbeddedPopup.popupManager.open('passwordRecovery.do?isSecondary=false&amp;deploymentId=ma-andover', 400, 400, 100)" tabindex="5" style="float: right"> 
I forgot my password 
</a> 


</td> 
</tr> 
<tr> 
<td width="1" class="logonTopPadding" style="float: left"> 
<input type="submit" tabindex="3" value="Log On" class="log-button"> 
</td> 
</tr> 

</tbody></table> 
</div> 
</td> 
</tr> 
</tbody></table> 

</center> 
<script> 
setTimeout(function(){window.location.reload(true);}, 1800000); 
</script> 
</form> 

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()); 
    } 

} 

該程序填充用戶名框並單擊「登錄」按鈕,但它不填充密碼框。我可以改變什麼來使這個程序工作?我懷疑密碼框的「type ='password'」屬性與問題有關,但如果我錯了,請糾正我。任何幫助表示讚賞。非常感謝你。

目標頁面:https://ma-andover.myfollett.com/aspen/home.do

這是我的輸出,在情況下,它可能會有所幫助:直到你鍵入username領域的東西

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 
+0

您使用的是哪個版本的htmlunit? – alkis

回答

3

密碼字段被禁用。 通過設置用戶名中的值不會觸發管理啓用密碼字段的事件。下面

工作

public static void main(String[] args) { 
    WebClient webClient = new WebClient(); 
    try { 
     HtmlPage page = (HtmlPage) webClient 
       .getPage("https://ma-andover.myfollett.com/aspen/logon.do"); 
     HtmlForm form = page.getFormByName("logonForm"); 
     form.getInputByName("username").setValueAttribute("myUsername"); 
     HtmlInput passWordInput = form.getInputByName("password"); 
     passWordInput.removeAttribute("disabled"); 
     passWordInput.setValueAttribute("myPassword"); 

     page = form.getInputByValue("Log On").click(); // works fine 

     System.out.println(page.asText()); 
    } catch (Exception e) { 
     e.printStackTrace(); 
    } finally { 
     webClient.close(); 
    } 
} 

輸出是

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 

Invalid login. 
OK 
+0

謝謝你這麼快回答我的問題。用戶名框被填入密碼框之前,所以我不明白爲什麼它被禁用,但你的行「passWordInput.removeAttribute(」disabled「);」似乎工作。登錄現在可以正常工作。 –

+0

@alkis我嘗試了你的代碼,但它引發了一個版本error.'在線程「main」中的異常java.lang.UnsupportedClassVersionError:com/gargoylesoftware/htmlunit/html/HtmlPage:Unsupported major.minor version 52.0'.I am java 1.7和' net.sourceforge.htmlunit 的HtmlUnit 2.27 '。什麼可能是原因是什麼?任何幫助表示讚賞 – Ricky

+0

@Ricky這是一個很老的答案。我不記得我用過的版本。請使用您正在使用的版本和代碼打開一個新問題。其他人可能會遇到同樣的問題,並可能有一個簡單的解決方案。 – alkis

1

要自動處理JavaScript的,你應該使用type()代替。

try (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").type("myUsername"); 
    form.getInputByName("password").type("myPassword"); 

    page = form.getInputByValue("Log On").click(); 

    System.out.println(page.asText()); 
    }