Java - 如何使用HtmlUnit登录网站?

5
我正在编写一个Java程序,用于登录我所在学校发布成绩的网站。
这是登录表单的网址: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 以下是我的输出,以防有用:
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

你使用的是哪个版本的HtmlUnit? - Alkis Kalogeris
3个回答

5

在输入用户名字段之前,密码字段是被禁用的。但是,仅仅设置用户名字段中的值并不能触发启用密码字段的事件。

下面的方式是有效的。

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

谢谢你这么快回答我的问题。用户名框在密码框之前已经填好了,所以我不明白为什么它会被禁用,但你的代码行"passWordInput.removeAttribute("disabled");"似乎解决了问题。现在登录正常运作。 - Irregular Square
@alkis 我尝试了你的代码,但是它抛出了一个版本错误。Exception in thread "main" java.lang.UnsupportedClassVersionError: com/gargoylesoftware/htmlunit/html/HtmlPage : Unsupported major.minor version 52.0。我正在使用Java 1.7和 <dependency> <groupId>net.sourceforge.htmlunit</groupId> <artifactId>htmlunit</artifactId> <version>2.27</version> </dependency>。可能是什么原因呢?感谢任何帮助。 - Ricky
@Ricky 这是一个非常老的答案。我不记得我使用的版本。请用你正在使用的版本和代码打开一个新的问题。其他人可能遇到了同样的问题,可能有一个简单的解决方案。 - Alkis Kalogeris

2
为了自动处理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());
}

0

我使用了:

final WebClient webClient = new WebClient())    
HtmlPage page = webClient.getPage("url");

((HtmlTextInput) page.getHtmlElementById("usernameID")).setText("Username");
page.getHtmlElementById("passwordID").setAttribute("value","Password");

page.getElementsByTagName("button").get(0).click();

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

我之所以那样点击按钮,是因为我的按钮没有id、name或value,但幸运的是它是页面上唯一的按钮。所以我只需获取所有按钮标签(只有一个),并选择列表中的第一个元素进行点击。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接