您好我想以编程方式填写登录表单并调用点击按钮登录某个网站。
我试图在此片段中实现此目的:
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:(function() { " +
"document.getElementsByTagName('user').value = " + "'mylogin'" + ";" +
"document.getElementsByTagName('password').value = " + "'mypassword'" + ";" +
"document.getElementsByTagName('input')[4].click(); " +
"})()");
}
}
不幸的是,上面的代码无效。我省略了什么吗? 感谢任何建议。