Search code examples
javayahoohtmlunit

HtmlUnit throws many exceptions on clicking one button


I'm trying to create program that signs in to Yahoo account. I'm using HtmlUnit in Java, but when the program is trying to cick Sign In button it throws a large number of various exceptions.

The button form is:

<form method="post" action="https://login.yahoo.com/config/login?" autocomplete="" name="login_form" onsubmit="return hash2(this)">
    <input type="hidden" name=".tries" value="1"/>
    <input type="hidden" name=".src" value=""/>
    <input type="hidden" name=".md5" value=""/>
    <input type="hidden" name=".hash" value=""/>
    <input type="hidden" name=".js" value=""/>
    <input type="hidden" name=".last" value=""/>
    <input type="hidden" name="promo" value=""/>
    <input type="hidden" name=".intl" value="us"/>
    <input type="hidden" name=".bypass" value=""/>
    <input type="hidden" name=".partner" value=""/>
    <input type="hidden" name=".u" value="eqn7kn96q7irv"/>
    <input type="hidden" name=".v" value="0"/>
    <input type="hidden" name=".challenge" value="rTRqt.vaVyBEJgxmMpkh0sqYx5Mz"/>
    <input type="hidden" name=".yplus" value=""/>
    <input type="hidden" name=".emailCode" value=""/>
    <input type="hidden" name="pkg" value=""/>
    <input type="hidden" name="stepid" value=""/>
    <input type="hidden" name=".ev" value=""/>
    <input type="hidden" name="hasMsgr" value="0"/>
    <input type="hidden" name=".chkP" value="Y"/>
    <input type="hidden" name=".done" value="http://my.yahoo.com"/>
    <input type="hidden" name=".pd" value="_ver=0&c=&ivt=&sg="/>
    <input type="hidden" name="pad" id="pad" value="3"/>
    <input type="hidden" name="aad" id="aad" value="3"/>
    <div id="inputs">
    <div id="fun"/>
    <div id="persistency">
    <div id="submit">
        <button type="submit" id=".save" name=".save" class="primaryCta" tabindex="5"> Sign In </button>
    </div>
</form>

and my java code:

import com.gargoylesoftware.htmlunit.WebClient;
import com.gargoylesoftware.htmlunit.html.HtmlForm;
import com.gargoylesoftware.htmlunit.html.HtmlPage;
import com.gargoylesoftware.htmlunit.BrowserVersion;
import com.gargoylesoftware.htmlunit.Page;
import com.gargoylesoftware.htmlunit.RefreshHandler;
import com.gargoylesoftware.htmlunit.html.HtmlButton;
import java.io.IOException;
import java.net.URL;

public class VirtualWebBrowser {

    public static void clickAuthorizeButton(String url, String login, String password) throws Exception {

        WebClient webClient = new WebClient(BrowserVersion.FIREFOX_3_6);
        webClient.setThrowExceptionOnScriptError(false);

        webClient.setRefreshHandler(new RefreshHandler() {
            public void handleRefresh(Page page, URL url, int arg) throws IOException {
                System.out.println("handleRefresh");
            }
        });

        HtmlPage loginPage = (HtmlPage) webClient.getPage(url);
        HtmlForm liginForm = loginPage.getFormByName("login_form");

        liginForm.getInputByName("login").setValueAttribute(login);
        liginForm.getInputByName("passwd").setValueAttribute(password);

        HtmlButton signInButton = liginForm.getButtonByName(".save");
        HtmlPage nextPage = (HtmlPage) signInButton.click();

        webClient.closeAllWindows();
    }
}

All exceptions is in the line HtmlPage nextPage = (HtmlPage) signInButton.click();

After this line program is logged in Yahoo account but these all exceptions somehow affects all program work that it works unstably. What should I do to avoid this situation or at least catch all exceptions?


Solution

  • From the errors you pasted it looks like one of those cases where the HTMLUnit Javascript implementation differs from the one in browser. I also wouldn't be surprised if Yahoo didn't want people to perform automatic login into their properties.

    Instead of wrestling with Javascript here I would try the following things:

    1. Look at HTTP communication using a sniffer (like HttpFox) and try to send login request using HTTPURLConnection or HTTPClient.
    2. See if you can login using the API. I know Yahoo provides OpenID, they also might provide OAuth authentication, see http://developer.yahoo.com/oauth/
    3. Use Selenium to login using real browser.