-->

Login to Google Account using HtmlUnit

2020-04-28 07:31发布

问题:

I'm trying to login to Google Acccount through HtmlUnit, but still something is wrong and I'm getting login page. What I'm doing wrong?

  1. Set email
  2. Click next button
  3. Set password
  4. Click login button
  5. Go to GMail page and it's still login page (output below)

My example code:

        WebClient client = new WebClient(BrowserVersion.CHROME);
        client.setHTMLParserListener(HTMLParserListener.LOG_REPORTER);
        client.setJavaScriptEngine(new JavaScriptEngine(client));
        client.getOptions().setJavaScriptEnabled(true);
        client.getCookieManager().setCookiesEnabled(true);
        client.getOptions().setThrowExceptionOnScriptError(false);
        client.getOptions().setThrowExceptionOnFailingStatusCode(false);
        client.setAjaxController(new NicelyResynchronizingAjaxController());
        client.getCache().setMaxSize(0);
        client.getOptions().setRedirectEnabled(true);

        String url = "https://accounts.google.com/login?hl=en#identifier";
        HtmlPage loginPage = client.getPage(url);
        client.waitForBackgroundJavaScript(1000000);

        HtmlForm loginForm = loginPage.getFirstByXPath("//form[@id='gaia_loginform']");
        List<HtmlInput> buttonInputs = loginForm.getInputsByValue("signIn");
        HtmlInput nextButton = Iterables.getFirst(buttonInputs, null);
        HtmlInput loginButton = Iterables.getLast(buttonInputs);
        Thread.sleep(2000);

        //setup email
        HtmlInput emailInput = loginForm.getInputByName("Email");
        emailInput.setValueAttribute(emailAddress);
        Thread.sleep(2000);

        //click next button
        nextButton.click();
        client.waitForBackgroundJavaScript(1000000);
        Thread.sleep(2000);

        //setup password
        HtmlInput passwordInput = loginForm.getInputByName("Passwd");
        passwordInput.setValueAttribute(password);

        //click login button
        loginButton.click();
        client.waitForBackgroundJavaScript(1000000);
        Thread.sleep(2000);

        HtmlPage gmailPage = client.getPage("https://mail.google.com/mail/u/0/#inbox");
        log.info(gmailPage.asText());

after all I'm getting output

2016-07-12 01:36:47 INFO  GoogleAccountClient:91 - Gmail

One account. All of Google.
 Sign in to continue to Gmail

 Next Need help?

Sign inchecked

Create account
 One Google Account for everything Google

About Google
 Privacy
 Terms
 Help

‪English (United States)‬

 identifier

I forgot something obvious?

I also tryied to click buttons by javascript

loginPage.executeJavaScript("document.getElementById('next').click()");
loginPage.executeJavaScript("document.getElementById('signIn').click()");

回答1:

I think what you did wrong was when you were looking for the "next" and the "sign in" buttons.

I'm pretty sure you need to do: List<HtmlInput> buttonInputs = loginForm.getInputsByName("signIn");

Instead, you put List<HtmlInput> buttonInputs = loginForm.getInputsByValue("signIn");

which isn't right because both the buttons have the same name of "signIn" when the "next button" has a value of "Next" and the "sign in" button has a value of "Sign in".