I am trying to login to a website programmatically using JavaScript.This is the HTML code of login form.
<form action="https://www.example.com/login/index.php" method="post" id="login" autocomplete="off" >
<div class="loginform">
<div class="form-label"><label for="username">Username</label></div>
<div class="form-input">
<input type="text" name="username" id="username" size="15" value="" />
</div>
<div class="form-label"><label for="password">Password</label></div>
<div class="form-input">
<input type="password" name="password" id="password" size="15" value="" autocomplete="off" />
<input type="submit" id="loginbtn" value="Login" />
</div>
</div>
I tried to login the website using webview in android application.I used
webView.loadUrl("http://www.example.com/info");
webView.setVisibility(View.INVISIBLE);
webView.setWebViewClient(new WebViewClient() {
public void onPageFinished(WebView view, String url) {
webView.loadUrl("javascript: {" +
"document.getElementById('username').value = '"+user+"';" +
"document.getElementById('password').value = '"+pass+"';" +
"document.getElementById('loginbtn').click();" +
"};");
}
public void onPageFinished2(WebView view, String url) {
webView.loadUrl(url);
}
});
webView.setVisibility(View.VISIBLE);
webView.clearCache(true);
webView.clearHistory();
}
But the webview only displayed uesrname and password in input fields.No button click happened.What's wrong with my code?