Can't log in to Google using HtmlUnit - Can

2019-08-13 07:56发布

问题:

Has anyone successfully logged into Google using HtmlUnit recently? I used to be able to, but since upgrading the BrowserVersion my WebClient uses, I am locked out. Many thanks if you can help me as I am really stuck!

I have been using BrowserVersion.CHROME from HtmlUnit-2.18. Login is a two-step process involving two forms. The first form has just the email field and the ‘Next’ button. After entering an email address and clicking ‘Next’, the second form displays. This form has the password field and the ‘Sign in’ button to complete the login process.

Here is the code I am using. myGoogleSitesHomeWebpage is still the login webpage when it should be my home web page:

HtmlTextInput email = (HtmlTextInput) webpage.getElementById("Email");
email.setValueAttribute("MyEmailAddress");
HtmlSubmitInput nextButton = webpage.getElementById("next");
newWebpage  = clickElement(nextButton);
webclient.waitForBackgroundJavaScriptStartingBefore(8000);
HtmlPasswordInput password = (HtmlPasswordInput) newWebpage.getElementById("Passwd-hidden");
password.setValueAttribute("MyPassword");
HtmlSubmitInput signInButton = newWebpage.getElementById("signIn");
myGoogleSitesHomeWebpage  = clickElement(signInButton);
webclient.waitForBackgroundJavaScriptStartingBefore(8000);

clickElement(nextButton) does not advance to the second form with the ‘Sign in’ button. It has my Google Sites web page I am trying to log into and my email correctly set up in the second form, but still with the link showing ‘Next’. I also think the input elements id="Email” and id="Passwd-hidden” are wrong and should be id="Email-hidden” and id="Passwd”. It seems that the second form I get is still the one for entering the email address and not the one for entering the password. Here are the relevant parts of the second form:

<form novalidate="" method="post" action="https://accounts.google.com/ServiceLoginAuth" id="gaia_loginform">
<input name="continue" type="hidden" value="https://sites.google.com/site/MyGoogleSitesHomePageAddress"/>
<input id="Email" name="Email" placeholder="Enter your email" type="email" value="MyEmailAddress" spellcheck="false" autofocus=""/>
<input id="Passwd-hidden" type="password" spellcheck="false" class="hidden"/>
<input id="next" name="signIn" class="rc-button rc-button-submit" type="submit" value="Next"/>
<input id="signIn" name="signIn" class="rc-button rc-button-submit" type="submit" value="Sign in"/>
</form>

Could anyone tell me what I am doing wrong and show me the code I should be using to do the complete login process? Many thanks, that would help me so much!

回答1:

It works with me with CHROME BrowserVersion.

Below is the complete case, which also gives 'OK'

    try (final WebClient webClient = new WebClient(BrowserVersion.CHROME)) {
        HtmlPage page = webClient.getPage("http://www.google.com");
        HtmlAnchor a = page.getHtmlElementById("gb_70");
        page = a.click();
        HtmlForm form = (HtmlForm) page.getElementById("gaia_loginform");
        HtmlTextInput email = (HtmlTextInput) page.getElementById("Email");
        email.setValueAttribute(myEmailAddress);
        HtmlSubmitInput nextButton = page.getHtmlElementById("next");
        System.out.println(nextButton.asXml());
        HtmlPage newWebpage = nextButton.click();
        String status = newWebpage.getWebResponse().getStatusMessage();
        System.out.println(status);
    }