2013-10-28 67 views
1

以下是我已經使用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> 

回答

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

你會發現你要尋找的是元素沒有在形式上。

+0

我可以看到它在輸出'<標籤= 「USER_LOGIN」> 用戶名
' – chility

+0

由於HtmlUnit和我一直在告訴你... :)名稱爲'user_login'的NO屬性...在您的評論中它具有值'log' :) –

+0

哦...但是我只是使用了'log'和'pwd',我仍然遇到了一個可怕的例外:'( – chility