我想在java中使用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("xxx@yahoo.com");
            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");
            //----------------------------------------------------------------
    }
}
在 com.att 的 java.util.ArrayList.get(Unknown Source) 处的 java.util.ArrayList.RangeCheck(Unknown Source) 处的线程“main”java.lang.IndexOutOfBoundsException 中的输出 1 异常:索引:0,大小:0。 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("yovan_05@yahoo.com");
    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)