Fill fields in webview automatically

前端 未结 6 1710
小蘑菇
小蘑菇 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:01

    Thanks all for your answer, it helped me, but didn't work.

    It was allways opening a white page until i found this :

    https://stackoverflow.com/a/25606090/3204928

    So here complete solution, mixing all infos found here and there :

    1) first of all you have to enable DOM storage, if you don't do that, .GetElementByXXX will return nothing (you have to do it before loading the page)

    myWebView.getSettings().setDomStorageEnabled(true);
    

    2)Your last Javascript call on GetElementByXXX MUST store the result in a variable

    Exemple 1 :

    _webview.loadUrl("javascript:var uselessvar =document.getElementById('passwordfield').value='"+password+"';");
    

    here only one call (only one semi-colon) so we immediatly store the result in 'uselessvar'

    Example 2 : see user802467 answer

    here there is 3 calls (one for login field, one for password field, one to submit button), only the last call need to be store, it's done in 'frms'

    Javascript programmers should easily explain this behaviour...

    hope this will help

    0 讨论(0)
  • 2020-11-27 05:06

    This worked for me to fill form values and submitting the form:

    webView.loadUrl("javascript: {" +
                "document.getElementById('username').value = '"+uname +"';" +
                "document.getElementById('password').value = '"+password+"';" +
                "var frms = document.getElementsByName('loginForm');" +
                "frms[0].submit(); };");
    
    0 讨论(0)
  • 2020-11-27 05:06

    It works for me

    webView.loadUrl("javascript:var uselessvar =document.getElementById('regno').value='"+mob+"';",null);
    webView.loadUrl("javascript:var uselessvar =document.getElementById('passwd').value='"+pass+"';",null);
    
    0 讨论(0)
  • 2020-11-27 05:08

    I tried @user802467 solution.But there was a different behaviour in 2 things

    1. If I stored ONLY the last javascript call in a variable, it was not filling in the fields. Instead if I stored all the three calls in variables, it did
    2. For some reason my form was not being submitted using submit(). But instead of submitting the form, if I clicked on the submit button using button.click(), I didnot need to store all the three calls and everything worked perfectly!

    Here is what I used (but didnt work)

    view.loadUrl("javascript: var x = document.getElementById('username').value = '" + username + "';" +
                            "var y = document.getElementById('password').value = '" + password + "';" +
                            "var form1 = document.getElementById('loginform');" +
                            "form1[0].submit(); ");
    

    Here is the code that worked for me

    view.loadUrl("javascript:  document.getElementById('username').value = '" + username + "';" +
                               " document.getElementById('password').value = '" + password + "';" +
                               "var z = document.getElementById('submitbutton').click();"
               );
    
    0 讨论(0)
  • 2020-11-27 05:15

    You don't need to use "java commands"... but instead JavaScript... for instance:

    String username = "cristian";
    webview.loadUrl("javascript:document.getElementById('username').value = '"+username+"';");
    

    So basically, what you have to do is a big string of JavaScript code that will get those fields and put values on them; also, you can enable/disable the submit button from JavaScript.

    0 讨论(0)
  • 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<String>() {
                @Override
                public void onReceiveValue(String s) {
    
                }
            });
        } else {
            view.loadUrl(js);
        }
    }
    

    });

    0 讨论(0)
提交回复
热议问题