问题
I want to login to a website (http://www.orkut.com) through
com.gargoylesoftware.htmlunit.WebClient
But when I click on the "Submit" button, it doesn't take me to the expected page that should come after login. Instead it returns the same login page again. In clear sense, there is some problem in login. When I try the same code with sites that doen't have javascript, it works fine so I think I am not able to handle scripts.
I am trying using the follwoing code:
public static void main(String[] args) {
final WebClient webClient = new WebClient();
try {
HtmlPage loginPage = webClient.getPage(new URL("https://www.google.com/accounts/ServiceLogin?service=orkut&hl=en-US&rm=false&continue=http%3A%2F%2Fwww.orkut.com%2FRedirLogin%3Fmsg%3D0%26page%3Dhttp%253A%252F%252Fwww.orkut.co.in%252FHome.aspx&cd=IN&passive=true&skipvpage=true&sendvemail=false"));
System.out.println(loginPage.getTextContent());
List<HtmlForm> forms = loginPage.getForms();
HtmlForm loginForm = forms.get(0);
HtmlInput username = loginForm.getInputByName("Email");
HtmlInput password = loginForm.getInputByName("Passwd");
HtmlInput submit = loginForm.getInputByName("signIn");
username.setNodeValue("username");
password.setNodeValue("password");
HtmlPage homePage = submit.click();
Thread.sleep(10 * 1000);
System.out.println(homePage.getTextContent());
}catch(Exception ex) {
ex.printStackTrace();
}
}
When we do click on the "submit" button, in actual it calls first this function
onsubmit="return(gaia_onLoginSubmit());"
specified as the attribute of the form below
<form id="gaia_loginform" action="https://www.google.com/accounts/ServiceLoginAuth?service=orkut" method="post"
onsubmit="return(gaia_onLoginSubmit());">
Can anyone help me in this.
NOTE: I WILL PAY FOR THE SOLUTION
回答1:
According to their site the JavaScript support is provided by Mozilla Rhino, so maybe all you need is to add it to your classpath (and perhaps fiddle with some configurations).
Also, HtmlUnit has professional support
来源:https://stackoverflow.com/questions/1536896/apache-htmlunit-problem-in-handling-javascript