问题
I want to automated the google login and accept oauth to get the authentication code.
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.
回答1:
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
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();
来源:https://stackoverflow.com/questions/33461077/htmlunit-google-login