Programmatically login on website
Hi i would like to programmatically fill the login forms and invoke click button to login on some website.
Im trying to achieve this like at this snippet:
public class LoginActivity extends Activity {
WebView mWebView;
Button btn;
EditText txtLogin;
EditText txtPassword;
WebView webview;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
btn = (Button)this.findViewById(R.id.button1);
txtLogin = (EditText)this.findViewById(R.id.txtLogin);
txtPassword = (EditText)this.findViewById(R.id.txtPassword);
webview = (WebView)findViewById(R.id.webView1);
webview.setWebViewClient(new WebClient());
webview.getSettings().setJavaScriptEnabled(true);
btn.setOnClickListener(new OnClickListener()
{
public void onClick(View v)
{
OpenWebContent();
}
});
}
public void OpenWebContent()
{
webview.loadUrl("http://www.www.com");
}
public class WebClient extends WebViewClient {
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
@Override
public void onPageFinished(WebView view, String url)
{
view.loadUrl("javascript:(func开发者_StackOverflow中文版tion() { " +
"document.getElementsByTagName('user').value = " + "'mylogin'" + ";" +
"document.getElementsByTagName('password').value = " + "'mypassword'" + ";" +
"document.getElementsByTagName('input')[4].click(); " +
"})()");
}
}
Unfortunetly the code above not working. Did i omitted something? Thank for any suggestion.
First of all - not all javascript methods are supported (although i couldn't find a list of supported and unsupported ones).
In this case i think you should follow this easy example.
精彩评论