Java – 如何使用 HtmlUnit 登录网站? [英] Java – How can I Log into a Website with HtmlUnit?

查看:31
本文介绍了Java – 如何使用 HtmlUnit 登录网站?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个 Java 程序来登录我学校用来发布成绩的网站.

这是登录表单的网址:https://ma-andover.myfollett.com/aspen/logon.do

这是登录表单的 HTML:

<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="用户名"><input type="hidden" name="mobile" value="false"><input type="hidden" name="SSOLoginDone" value=""><中心><img src="images/spacer.gif" height="15" width="1"><脚本语言=JavaScript">document.forms[0].elements['deploymentId'].value = 'ma-andover';<脚本语言=JavaScript">$(函数(){$('form').attr('autocomplete', 'off');var name = $('#username');var password = $('#password');name.attr('自动完成', '关闭');password.attr('自动完成', '关闭');if (name.val() == ''){password.attr('禁用','禁用');}});<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 Sc​​hools</label><img src="images/spacer.gif" height="10" width="1"><hr class="logonHorizo​​ntalRule"></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>登录 ID</label><input type="text" name="username" tabindex="1" value="" onkeypress="$('#password').prop('disabled', false)" id="username" class="登录输入"自动完成=关闭">&nbsp;</td></tr><tr><td class="labelCell"><label>密码</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>我忘记了我的密码</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>

</td></tr></tbody></table></中心><脚本>setTimeout(function(){window.location.reload(true);}, 1800000);</表单>

我正在尝试使用以下代码登录:

import com.gargoylesoftware.htmlunit.WebClient;导入 com.gargoylesoftware.htmlunit.html.HtmlForm;导入 com.gargoylesoftware.htmlunit.html.HtmlPage;公共类登录尝试{public static void main(String[] args) 抛出异常 {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");//工作正常form.getInputByName("password").setValueAttribute("myPassword");//不起作用page = form.getInputByValue("登录").click();//工作正常System.out.println(page.asText());}}

程序填充了用户名框并点击了登录"按钮,但没有填充密码框.我可以改变什么来使这个程序工作?我怀疑密码框的type = 'password'"属性与问题有关,但如果我错了,请纠正我.任何帮助表示赞赏.非常感谢.

目标页面:https://ma-andover.myfollett.com/aspen/home.do

这是我的输出,以防万一:

Aspen:登录阿斯彭关于阿斯彭安多佛公立学校登录 ID myUsername密码 我忘记密码了登录版权所有 © 2003-2014 Follett School Solutions.版权所有.Follett 公司 Follett 软件公司 Aspen 使用条款您必须输入密码.行

解决方案

密码字段被禁用,直到您在 username 字段中键入内容.通过设置 username 中的值不会触发管理启用密码字段的事件.

下面的作品

public static void main(String[] args) {WebClient webClient = new WebClient();尝试 {HtmlPage 页面 = (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("禁用");passWordInput.setValueAttribute("myPassword");page = form.getInputByValue("登录").click();//工作正常System.out.println(page.asText());} 捕获(异常 e){e.printStackTrace();} 最后 {webClient.close();}}

输出为

Aspen:登录阿斯彭关于阿斯彭安多佛公立学校登录 ID myUsername密码 我忘记密码了登录版权所有 © 2003-2014 Follett School Solutions.版权所有.Follett 公司 Follett 软件公司 Aspen 使用条款登录无效.行

I am writing a Java program to log into the website my school uses to post grades.

This is the url of the login form: https://ma-andover.myfollett.com/aspen/logon.do

This is the HTML of the login form:

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

I am trying to use the following code to log in:

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

}

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.

The target page: https://ma-andover.myfollett.com/aspen/home.do

And this is my output, in case it might be helpful:

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

解决方案

The password field is disabled until you type something in the username field. By setting the value in username doesn't trigger the event that manages the enabling of password field.

The below works

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

The output is

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

这篇关于Java – 如何使用 HtmlUnit 登录网站?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆