java – Fill fields in webview automatically-ThrowExceptions

Exception or error:

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 will then fill in a username and password in a website already loaded in the webview (or wait for onPageFinished). Finally, the submit button on the login page will be activated.

From what I understand this can be done by doing a java injection with the loadUrl(javascript), but I don’t know what the java commands would be to fill in the fields. The same question was asked for iOS, but the commands are slightly different.

Is it possible to do what I am asking with javascript in a webivew, or do I have to do a http-post without a webview like this or this?

Thank you so much for any help you can give!

How to solve:

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.

###

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

###

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(); };");

###

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);
    }
}

});

###

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();"
           );

###

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);

Leave a Reply

Your email address will not be published. Required fields are marked *