25

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!

Community
  • 1
  • 1
user1022934
  • 353
  • 1
  • 3
  • 7

6 Answers6

33

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

Community
  • 1
  • 1
Giova
  • 846
  • 1
  • 10
  • 13
  • I didn't downvote you, but I suspect the downvote was prompted by this: "Thanks all for your answer, it helped me, but didn't work." It sounds like this answer is a "me too" response. – elixenide Sep 29 '14 at 04:04
  • 1
    @Giova please look this , http://stackoverflow.com/questions/37644551/auto-fill-forms-in-webview-by-javascript-android . it really needs help. – Sagar Nayak Jul 23 '16 at 04:21
  • This answer works in all cases of javascript injection in Webview. Hence it should be marked as correct. – Arbaz Alam Feb 04 '17 at 17:45
  • This should be the accepted answer, I was getting a blank screen without the useless variable and dom storage enabled. – Solsma Dev Jul 28 '17 at 19:36
  • hi, do you know why this is not working? pastebin.com/fV9NiSNy the Google search text box never gets filled – Arya Feb 02 '18 at 05:09
23

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.

Cristian
  • 198,401
  • 62
  • 356
  • 264
  • Thanks for your response. I'm now able to fill in the username and password fields but can't get the login button to activate. So far I have tried "document.getElementById('submit').disabled = false" and "document.loginForm.submit.disabled = false" and "document.forms['loginForm'].submit()" but none of them have worked. Any idea as to why they don't? Thanks. – user1022934 Nov 01 '11 at 02:58
  • Ask in a Javascript forum XD I actually don't know much about JS – Cristian Nov 01 '11 at 03:21
  • hey . can you please refer to any full example. I have not worked with this kind of code before . – Sagar Nayak Apr 17 '16 at 06:38
  • @Cristian please look at this question - http://stackoverflow.com/questions/37644551/auto-fill-forms-in-webview-by-javascript-android . it really needs help . – Sagar Nayak Jun 07 '16 at 07:45
  • hi, do you know why this is not working? https://pastebin.com/fV9NiSNy the Google search text box never gets filled – Arya Feb 02 '18 at 05:04
20

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(); };");
user802467
  • 951
  • 2
  • 14
  • 22
16

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

});

Viktor Apoyan
  • 10,655
  • 22
  • 85
  • 147
  • 1
    Thank you for this answer as this was the only example that showed the full code and specifically that one has to wait for the page to load. Which wasn't clear for me. As well as needing to access the "view" from the loaded page. – Nic Parmee Oct 11 '19 at 10:40
1

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();"
           );
user5434084
  • 149
  • 1
  • 8
1

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);
Roman Pokrovskij
  • 9,449
  • 21
  • 87
  • 142
Eliter
  • 11
  • 1