user1022934
user1022934

Reputation: 353

Fill fields in webview automatically

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!

Upvotes: 25

Views: 43972

Answers (6)

Eliter
Eliter

Reputation: 11

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

Upvotes: 1

user5434084
user5434084

Reputation: 149

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

Upvotes: 1

Viktor Apoyan
Viktor Apoyan

Reputation: 10755

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

});

Upvotes: 16

Giova
Giova

Reputation: 876

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

Upvotes: 33

user802467
user802467

Reputation: 961

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

Upvotes: 20

Cristian
Cristian

Reputation: 200090

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.

Upvotes: 23

Related Questions