14

I'm trying to fill Webforms from a Webview in Android. I've already found this piece of code here: Fill fields in webview automatically

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

Unfortunatly I dont understand where I have to open the page I want to fill in.

setContentView(R.layout.web);
final WebView mWebView = (WebView) findViewById(R.id.webview);
mWebView.getSettings().setJavaScriptEnabled(true);
mWebView.loadUrl(url);
String user="u";
String pwd="p";
mWebView.loadUrl("javascript:document.getElementById('username').value = '"+user+"';document.getElementById('password').value='"+pwd+"';");

When I try it this way, the site gets displayed but without any values in the forms.

Thanks in advance for helping

Community
  • 1
  • 1
Jay
  • 281
  • 1
  • 4
  • 13

4 Answers4

25

Android 4.4 new WebView have the issue with use loadUrl("javascript:") method, the paramter string will be url-decode before execution. You can try use evaluateJavascript() for API >= 19 and loadUrl() for API < 19. The code below is work for me.

    mWebView.loadUrl(url)
    WebSettings settings = mWebView.getSettings();
    settings.setJavaScriptEnabled(true);

    String js = "javascript:document.getElementById('username').value = '"+user+"';document.getElementById('password').value='"+pwd+"';";
    mWebView.setWebViewClient(new WebViewClient() {

        @Override
        public void onPageFinished(WebView view, String url) {
            super.onPageFinished(view, url);

            if (Build.VERSION.SDK_INT >= 19) {
                view.evaluateJavascript(js, new ValueCallback<String>() {
                    @Override
                    public void onReceiveValue(String s) {

                    }
                });
            } else {
                view.loadUrl(js);
            }
       });
    }

please refer: loadUrl("javascript:....") behaviour changed incompatibly in Android 4.4

awind
  • 361
  • 3
  • 3
  • Please consider editing your post to add more explanation about what your code does and why it will solve the problem. An answer that mostly just contains code (even if it's working) usually wont help the OP to understand their problem. – SuperBiasedMan Aug 20 '15 at 10:18
  • @awind can i get ajax response after send form data? form webpage display in webview from remote url – Mansukh Ahir Apr 16 '16 at 07:17
  • I would say the version check is overkill (since loadUrl still exists and functions in later versions, `evaluateJavascript` is only "required" if you need to get a return value), however apparently they introduced some bug so that it parses differently or something https://issuetracker.google.com/issues/36995865 , so this might be good still :) – rogerdpack Oct 10 '17 at 04:59
22

You should fill the values after the page has been loaded. This is an example using your code:

mWebView.loadUrl(url);
mWebView.getSettings().setJavaScriptEnabled(true);
mWebView.setWebViewClient(new WebViewClient() {
    public void onPageFinished(WebView view, String url) {
        String user="u";
        String pwd="p";
        view.loadUrl("javascript:document.getElementById('username').value = '"+user+"';document.getElementById('password').value='"+pwd+"';");
    }
});
gnpaolo
  • 350
  • 4
  • 6
  • i have tried by using your code.web page is loading but fields are not filled by that data.it's empty only.can you tell how to write javascript code for this. – user1213202 Mar 22 '13 at 06:01
  • You could have a different name for the element 'username' or 'password'. You should substitute those names with the actual field names of your web page. – gnpaolo Mar 25 '13 at 09:43
  • 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:37
  • The answer from awind should be considered the right answer! – pepan Dec 20 '16 at 21:15
  • 1
    i have tried but while i pass 1 id, it's working properly but while i pass 2 more id, by ";" its print the value instead of showing web page. Any idea what will be the issue? – Neal Jan 04 '17 at 10:42
2

Just Enable DomStorage and write "var x=" to string:

webview.getSettings().setJavaScriptEnabled(true);
web.getSettings().setDomStorageEnabled(true);

webview.loadUrl(urlString);
webview.setWebViewClient(new WebViewClient(){

public void onPageFinished(WebView view, String url){  
super.onPageFinished(view, url);
String js = "javascript:var x=document.getElementById('username').value = '"+user+"';var y=document.getElementById('password').value='"+pass+"';";

if (Build.VERSION.SDK_INT >= 19) {
        view.evaluateJavascript(js, new ValueCallback<String>() {
            @Override
            public void onReceiveValue(String s) {

            }
        });
    } else {
        view.loadUrl(js);
    }

view.loadUrl(js);
}
});
Denish Rana
  • 101
  • 2
  • 11
1

I had the same issue, and after following different explanations on stackOverflow, I succeed to make mine working.

here is my approach

webView.loadUrl(url);
        webView.getSettings().setDomStorageEnabled(true);
        webView.setWebViewClient(new WebViewClient(){
            public void onPageFinished(WebView view, String url) {
                String email="email@email.jp";

                //view.loadUrl("javascript:document.getElementById('email').value = '"+email+"'");
                view.loadUrl("javascript:document.forms[0].email.value = '"+email+"';");
                Log.d("email", "can not add email");
            }
        });

2 things : 1) you need to add this line webView.getSettings().setDomStorageEnabled(true); (reference: Android WebView always returns null for javascript getElementById on loadUrl)

2) you need to access the variable in your php code by using this view.loadUrl("javascript:document.forms[0].email.value = '"+email+"';"); as you can see in my code I used the solution proposed by @gnpaolo, but it didn't work for me so I commented it and use this one. (reference: How to inject a String into Android WebView)

Finally, just want to add that you do not need to create a special javascript.

one more thing the forms[0] is the position of the variable in the php form, and in my case, I have the email of the user, so I wrote view.loadUrl("javascript:document.forms[0].email.value = '"+email+"';");

Hope, this can help others.

Community
  • 1
  • 1
Edess Elder
  • 1,461
  • 13
  • 9