以下是我到目前为止使用HTMLUNIT库所做的事情 - 它实际上是单击按钮时发生的事件:
private void btnTestActionPerformed(java.awt.event.ActionEvent evt) {
try{
final WebClient webClient = new WebClient();
// Get the first page
final HtmlPage page1 = webClient.getPage("http://cashialize.com/wp-login.php");
// Get the form that we are dealing with and within that form,
// find the submit button and the field that we want to change.
final HtmlForm form = page1.getFormByName("loginform");
final HtmlSubmitInput button = form.getInputByName("wp-submit");
final HtmlTextInput userField = form.getInputByName("user_login");
final HtmlTextInput passField = form.getInputByName("passField");
// Change the value of the text field
userField.setValueAttribute("admin");
passField.setValueAttribute("blabla");
// Now submit the form by clicking the button and get back the second page.
final HtmlPage page2 = button.click();
webClient.closeAllWindows();
}catch(IOException e){
System.out.println(e);
}
}
我已经按照他们的指南进行了操作,但对我来说,执行登录似乎有点不可能。我正在使用netbeans 7.4
,这是我第一次使用GUI构建器。我通常都在研究eclipse(我仍然无法使用环境),但我确信它更容易。无论如何。我100%肯定我错过了一些非常小的东西,但我现在无法发现它。
这是一个可怕的例外,她对我没有任何意义:
Exception in thread "AWT-EventQueue-0" com.gargoylesoftware.htmlunit.ElementNotFoundException: elementName=[input] attributeName=[name] attributeValue=[user_login]
at com.gargoylesoftware.htmlunit.html.HtmlForm.getInputByName(HtmlForm.java:463)
at Main.btnTestActionPerformed(Main.java:95)
at Main.access$000(Main.java:18)
at Main$1.actionPerformed(Main.java:44)
at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2018)
at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2341)
at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:402)
at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:259)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:252)
at java.awt.Component.processMouseEvent(Component.java:6505)
at javax.swing.JComponent.processMouseEvent(JComponent.java:3321)
at java.awt.Component.processEvent(Component.java:6270)
at java.awt.Container.processEvent(Container.java:2229)
at java.awt.Component.dispatchEventImpl(Component.java:4861)
at java.awt.Container.dispatchEventImpl(Container.java:2287)
at java.awt.Component.dispatchEvent(Component.java:4687)
at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4832)
at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4492)
at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4422)
at java.awt.Container.dispatchEventImpl(Container.java:2273)
at java.awt.Window.dispatchEventImpl(Window.java:2719)
at java.awt.Component.dispatchEvent(Component.java:4687)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:735)
at java.awt.EventQueue.access$200(EventQueue.java:103)
at java.awt.EventQueue$3.run(EventQueue.java:694)
at java.awt.EventQueue$3.run(EventQueue.java:692)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:87)
at java.awt.EventQueue$4.run(EventQueue.java:708)
at java.awt.EventQueue$4.run(EventQueue.java:706)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:705)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:242)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:161)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:150)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:146)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:138)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:91)
修改
Mosty Mostacho建议的代码之后的控制台输出
<?xml version="1.0" encoding="UTF-8"?>
<html xmlns="http://www.w3.org/1999/xhtml" lang="en-US">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
<title>
Cashialize! You can do it! › Log In
</title>
<link rel="stylesheet" id="wp-admin-css" href="http://cashialize.com/wp-admin/css/wp-admin.min.css?ver=3.7" type="text/css" media="all"/>
<link rel="stylesheet" id="buttons-css" href="http://cashialize.com/wp-includes/css/buttons.min.css?ver=3.7" type="text/css" media="all"/>
<link rel="stylesheet" id="colors-fresh-css" href="http://cashialize.com/wp-admin/css/colors-fresh.min.css?ver=3.7" type="text/css" media="all"/>
Exception in thread "AWT-EventQueue-0" java.lang.ClassCastException: com.gargoylesoftware.htmlunit.html.HtmlPasswordInput cannot be cast to com.gargoylesoftware.htmlunit.html.HtmlTextInput
<meta name="robots" content="noindex,nofollow"/>
</head>
at Main.btnTestActionPerformed(Main.java:97)
<body class="login login-action-login wp-core-ui">
<div id="login">
<h1>
at Main.access$000(Main.java:18)
at Main$1.actionPerformed(Main.java:44)
<a href="http://wordpress.org/" title="Powered by WordPress">
Cashialize! You can do it!
</a>
</h1>
at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2018)
<form name="loginform" id="loginform" action="http://cashialize.com/wp-login.php" method="post">
at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2341)
at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:402)
<p>
<label for="user_login">
Username
at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:259)
<br/>
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:252)
at java.awt.Component.processMouseEvent(Component.java:6505)
<input type="text" name="log" id="user_login" class="input" value="" size="20"/>
</label>
</p>
<p>
<label for="user_pass">
at javax.swing.JComponent.processMouseEvent(JComponent.java:3321)
Password
<br/>
<input type="password" name="pwd" id="user_pass" class="input" value="" size="20"/>
at java.awt.Component.processEvent(Component.java:6270)
at java.awt.Container.processEvent(Container.java:2229)
</label>
</p>
at java.awt.Component.dispatchEventImpl(Component.java:4861)
at java.awt.Container.dispatchEventImpl(Container.java:2287)
at java.awt.Component.dispatchEvent(Component.java:4687)
at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4832)
at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4492)
at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4422)
<p class="forgetmenot">
<label for="rememberme">
<input name="rememberme" type="checkbox" id="rememberme" value="forever"/>
at java.awt.Container.dispatchEventImpl(Container.java:2273)
at java.awt.Window.dispatchEventImpl(Window.java:2719)
Remember Me
</label>
</p>
<p class="submit">
at java.awt.Component.dispatchEvent(Component.java:4687)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:735)
at java.awt.EventQueue.access$200(EventQueue.java:103)
at java.awt.EventQueue$3.run(EventQueue.java:694)
at java.awt.EventQueue$3.run(EventQueue.java:692)
at java.security.AccessController.doPrivileged(Native Method)
<input type="submit" name="wp-submit" id="wp-submit" class="button button-primary button-large" value="Log In"/>
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
<input type="hidden" name="redirect_to" value="http://cashialize.com/wp-admin/"/>
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:87)
<input type="hidden" name="testcookie" value="1"/>
at java.awt.EventQueue$4.run(EventQueue.java:708)
</p>
</form>
<p id="nav">
at java.awt.EventQueue$4.run(EventQueue.java:706)
at java.security.AccessController.doPrivileged(Native Method)
<a href="http://cashialize.com/wp-login.php?action=lostpassword" title="Password Lost and Found">
Lost your password?
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:705)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:242)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:161)
</a>
</p>
<script type="text/javascript">
//<![CDATA[
function wp_attempt_focus(){
setTimeout( function(){ try{
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:150)
d = document.getElementById('user_login');
d.focus();
d.select();
} catch(e){}
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:146)
}, 200);
}
wp_attempt_focus();
if(typeof wpOnload=='function')wpOnload();
//]]>
</script>
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:138)
<p id="backtoblog">
<a href="http://cashialize.com/" title="Are you lost?">
at java.awt.EventDispatchThread.run(EventDispatchThread.java:91)
← Back to Cashialize! You can do it!
</a>
</p>
</div>
<div class="clear">
</div>
</body>
</html>
答案 0 :(得分:4)
不要害怕,这是一个简单的问题:) HtmlUnit告诉你的例外是这一行:
final HtmlTextInput userField = form.getInputByName("user_login");
你找不到任何东西。换句话说,表单中没有包含name
值user_login
属性的输入。
我建议您在获取页面代码后立即输出HtmlUnit正在处理的实际代码:
final HtmlPage page1 = webClient.getPage("http://cashialize.com/wp-login.php");
System.out.println(page1.asXml());
您会发现您要查找的元素不在表单中。