2013-06-20 141 views
0

我想用java中的htmlunit登錄到我的yahoo電子郵件,我嘗試使用許多不同的方式登錄,但似乎沒有爲我工作。它似乎是提交按鈕的問題。請幫忙?HtmlUnit在登錄頁面拋出異常提交按鈕

代碼1:

import java.io.IOException; 
import java.net.MalformedURLException; 

import com.gargoylesoftware.htmlunit.BrowserVersion; 
import com.gargoylesoftware.htmlunit.FailingHttpStatusCodeException; 
import com.gargoylesoftware.htmlunit.WebClient; 
import com.gargoylesoftware.htmlunit.html.HtmlElement; 
import com.gargoylesoftware.htmlunit.html.HtmlPage; 
import com.gargoylesoftware.htmlunit.html.HtmlPasswordInput; 
import com.gargoylesoftware.htmlunit.html.HtmlTextInput; 

public class AnotherYahoo2 { 

    /** 
    * @param args 
    * @throws IOException 
    * @throws MalformedURLException 
    * @throws FailingHttpStatusCodeException 
    */ 
    public static void main(String[] args) throws FailingHttpStatusCodeException, MalformedURLException, IOException { 
     // TODO Auto-generated method stub 
     //---------------------------------Login Page--------------------------------- 
      WebClient webClient = new WebClient(BrowserVersion.FIREFOX_17); 
     HtmlPage PageLogin = webClient.getPage("https://login.yahoo.com/config/login?"); 



     HtmlElement submitButton = (HtmlElement) PageLogin.getByXPath("//div[@name='.save']").get(0); 
     HtmlTextInput name = (HtmlTextInput) PageLogin.getElementById("username"); 
     HtmlPasswordInput pass = (HtmlPasswordInput)PageLogin.getElementById("passwd"); 

      name.setText("[email protected]"); 
      pass.setText("exxx5"); 

      System.out.println("Logging in to site"); 
      //------------------------------------------------------------------------ 

      //---------------------------------Pass varified Page---------------------- 
      HtmlPage pagePassVarified = submitButton.click(); 
      System.out.println("Successfully Logged in to site"); 
      HtmlElement btnContinue = (HtmlElement) pagePassVarified.getElementById(".save"); 
      //--------------------------------------------------------- 

      //---------------------Home Page---------------------------------- 
      HtmlPage pageHome = btnContinue.click(); 
      System.out.println("Home Page accessed"); 
      //---------------------------------------------------------------- 
    } 

} 

輸出1個 異常在線程 「主」 java.lang.IndexOutOfBoundsException:索引:0,大小:0 在java.util.ArrayList.RangeCheck(未知來源) 在java.util.ArrayList.get(未知源) at com.att.temp.AnotherYahoo2.main(AnotherYahoo2.java:30)

代碼#2 ...我也嘗試過不同的方式,它也是失敗

public static void main(String[] args) throws Exception { 

    WebClient webClient = new WebClient(BrowserVersion.FIREFOX_17); 
    webClient.setThrowExceptionOnScriptError(false); 
    // Get the first page 
    HtmlPage page1 = (HtmlPage) webClient.getPage("https://login.yahoo.com/config/login?"); 

    // Get the form that we are dealing with and within that form, 
    // find the submit button and the field that we want to change. 
    HtmlForm form = page1.getFormByName("login_form"); 

    // Enter login and passwd 
    form.getInputByName("login").setValueAttribute("[email protected]"); 
    form.getInputByName("passwd").setValueAttribute("escobar05"); 

    // Click "Sign In" button/link 
    page1 = (HtmlPage) form.getInputByValue("submit").click(); 

    // I added the cookie section but this returns a null pointer exception  
    Set<Cookie> cookie = webClient.getCookieManager().getCookies(); 

    if(cookie != null){ 

     Iterator<Cookie> i = cookie.iterator(); 

     while (i.hasNext()) { 

     webClient.getCookieManager().addCookie(i.next()); 
     } 
    } 
    // Get page as Html 
    String htmlBody = page1.getWebResponse().getContentAsString(); 
    // Save the response in a file 
    String filePath = "c://temp//test_out.html"; 

    BufferedWriter bw = new BufferedWriter(new FileWriter(new File(filePath))); 
    bw.write(htmlBody); 
    bw.close(); 

    webClient.closeAllWindows(); 
    } 

輸出2

Exception in thread "main" com.gargoylesoftware.htmlunit.ElementNotFoundException:  elementName=[input] attributeName=[value] attributeValue=[submit] 
at com.gargoylesoftware.htmlunit.html.HtmlForm.getInputByValue(HtmlForm.java:794) 
at com.att.temp.AnotherYahoo.main(AnotherYahoo.java:41) 

回答

0

嘗試以下方法:

HtmlButton htmlButton = page.getFirstByXPath("//*[@id=\".save\"]") ; 
htmlButton.click();