HtmlUnit Google登录

问题描述:

我想要自动化google登录并接受oauth以获取验证码。HtmlUnit Google登录

我得到一个NullPointerExeption nextButton.click(),我找不到“下一个”按钮。

 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()); 

我已经试过这Can't log in to Google using HtmlUnit - Can't advance to web page for entering password 没有成功。

问题出在网站上,html页面不像标准浏览器。登录页面如下所示:https://lh6.ggpht.com/v19i1LtG-IdZKSZ-rhtuflJJmVmZM3gd3uauQQyLFvJxXTxYi4t8ygCQXwutu1nq69mmna8=w351

您还可以查看页面对象,您可以看到包含所有可用ID的idMap。

 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();