以编程方式点击Android WebView中的按钮

Click on button in Android WebView programmatically

本文关键字:按钮 WebView Android 编程 方式点      更新时间:2023-09-26

我正在尝试填写几个字段(工作)然后单击登录按钮(不工作)我已经尝试过:

document.getElementById('loginButton').click();"

document.forms['form'].submit();

按钮名称和表单名称是正确的,我做错了什么?

webview.setWebViewClient(new WebViewClient() {  
             @Override  
             public void onPageFinished(WebView view, String url)  
             {  
                 webview.loadUrl("javascript:(function() { " +  
                      "document.getElementById('coCode').value = 'CompanyCode';" +
                      "document.getElementById('loginName').value = 'LoginName';" +
                      "document.getElementById('password').value = 'Password';" +
                      //"document.forms['form'].submit();" + //Doesn't Work
                      //"document.getElementById('loginButton').click();" + //Doesn't Work
                    "})()");
             }  
         });

我想明白了,下面的方法对我有效:

webview.loadUrl("javascript:WebForm_DoPostBackWithOptions(
  new WebForm_PostBackOptions('loginButton', '', true, '', '', false, true))");