[FIXED] How can I autofill a websites fields/forms in a WebView in Android Studio?

Issue

I want to autofill for example Facebook’s login fields(email and password) or Google’s search field. I have gone through many questions about autofill, including this.

This is my code at the moment, which opens up the website in the WebView, but doesn’t fill out anything. And for the getElementById I inspected the facebook’s email and password fields and their ID’s were email and pass.

    webView = (WebView) findViewById(R.id.webView1);
    webView.loadUrl("https://www.facebook.com");
    WebSettings webSettings = webView.getSettings();
    webSettings.setJavaScriptEnabled(true);
    webSettings.setDomStorageEnabled(true);
    webView.setWebViewClient(new WebViewClient() {
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            view.loadUrl(url);
            return true;
        }


        public void onPageFinished(WebView view, String url) {
            String user = "udb";
            String pwd = "abcdefg";
            view.loadUrl("javascript:document.getElementById('email').value = '" + user + "';document.getElementById('pass').value='" + pwd + "';");
            System.out.println("AUTOFILL");
        }
    });

and it gives me this error

I/chromium: [INFO:CONSOLE(1)] "Uncaught TypeError: Cannot set property ‘value’ of null", source: (1)

I don’t know much about javaScript though.

Solution

Well I still can’t get facebook to work, but I got google sign in to work and some other website register/sign ins. SO basically you need to inspect the website’s elements(by using google chrome on desktop) and then get the element id;

this is the code to fill out a form/field:

    webView.loadUrl("javascript:(function() { document.getElementById('last_name').value = '" + lName + "'; ;})()");

and this is the code to click a button:

    webView.loadUrl("javascript:(function() { var z = document.getElementById('signInButton').click(); })()");

Oh and I used my phone to do this, because with emulator it didn’t do anything.

EDIT: I actually got facebook to kinda work by inspecting the mobile.facebook.com website, but there was no ID for email, only for password and login button and some other elements.

Answered By – Oki Doki

Answer Checked By – Candace Johnson (Easybugfix Volunteer)

Leave a Reply

(*) Required, Your email will not be published