HtmlUnit Google登录 [英] HtmlUnit Google Login

查看:135
本文介绍了HtmlUnit Google登录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想自动执行google登录并接受oauth来获取验证码.

I want to automated the google login and accept oauth to get the authentication code.

我通过nextButton.click()得到了一个N​​ullPointerExeption,找不到下一个"按钮.

I get a NullPointerExeption by nextButton.click(), I can't find the "next" Button.

        WebClient webClient = new WebClient(); //BrowserVersion.FIREFOX_38
        HtmlPage page = webClient.getPage("https://accounts.google.com/o/oauth2/auth?");            

        HtmlTextInput email = (HtmlTextInput)page.getElementById("Email");
        email.setValueAttribute(emailAddress);
        HtmlSubmitInput nextButton = (HtmlSubmitInput)page.getElementById("next");

        HtmlPage newPage = (HtmlPage)nextButton.click();
        webClient.waitForBackgroundJavaScriptStartingBefore(8000);

        HtmlTextInput passwd = (HtmlTextInput)page.getElementById("Passwd");
        passwd.setValueAttribute(password);
        HtmlSubmitInput signIn = (HtmlSubmitInput)page.getElementById("signIn");

        HtmlPage pageSucces = (HtmlPage)signIn.click();
        webClient.waitForBackgroundJavaScriptStartingBefore(8000);

        HtmlSubmitInput submitAccess = (HtmlSubmitInput)page.getElementById("submit_approve_access");
        HtmlPage pageAccess = (HtmlPage)submitAccess.click();
        webClient.waitForBackgroundJavaScriptStartingBefore(8000);

        HtmlTextInput code = (HtmlTextInput)page.getElementById("code");
        System.out.println(code.getText());

我已经尝试过

I've tried this Can't log in to Google using HtmlUnit - Can't advance to web page for entering password without success.

推荐答案

问题出在网站上,html页面与标准浏览器不一样.登录页面如下所示: 19://1.com rhtuflJJmVmZM3gd3uauQQyLFvJxXTxYi4t8ygCQXwutu1nq69mmna8 = w351

The problem was the website, the html page is not like the standard browser. The login page looks like this: https://lh6.ggpht.com/v19i1LtG-IdZKSZ-rhtuflJJmVmZM3gd3uauQQyLFvJxXTxYi4t8ygCQXwutu1nq69mmna8=w351

您还可以查看页面对象,在那里您可以看到具有所有可用ID的idMap.

You can also look into the page object there you can see the idMap with all available IDs.

        HtmlTextInput email = (HtmlTextInput)page.getElementById("Email");
        email.setValueAttribute(emailAddress);
        HtmlPasswordInput passwd = (HtmlPasswordInput)page.getElementById("Passwd");
        passwd.setValueAttribute(password);
        HtmlSubmitInput signInButton = (HtmlSubmitInput)page.getElementById("signIn");

        webClient.waitForBackgroundJavaScriptStartingBefore(8000);
        HtmlPage newPage = (HtmlPage)signInButton.click();

这篇关于HtmlUnit Google登录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆