Fill fields in webview automatically

前端 未结 6 1715
小蘑菇
小蘑菇 2020-11-27 04:26

I have seen this question floating around the internet, but I haven\'t found a working solution yet. Basically, I want to load my app and press a button; the button action w

6条回答
  •  一向
    一向 (楼主)
    2020-11-27 05:18

    Here is complete code which works for me (Bitbucket):

    webView.getSettings().setJavaScriptEnabled(true);
    webView.getSettings().setDomStorageEnabled(true);
    webView.loadUrl("http://example.com/");
    webView.setWebViewClient(new WebViewClient(){
    @Override
    public void onPageFinished(WebView view, String url) {
        super.onPageFinished(view, url);
    
        final String password = "password";
        final String username = "username";
        final String answer = 5;
    
        final String js = "javascript:" +
                "document.getElementById('password').value = '" + password + "';"  +
                "document.getElementById('username').value = '" + username + "';"  +
                "var ans = document.getElementsByName('answer');"                  +
                "ans[0].value = '" + answer + "';"                                 +
                "document.getElementById('fl').click()";
    
        if (Build.VERSION.SDK_INT >= 19) {
            view.evaluateJavascript(js, new ValueCallback() {
                @Override
                public void onReceiveValue(String s) {
    
                }
            });
        } else {
            view.loadUrl(js);
        }
    }
    

    });

提交回复
热议问题