admin管理员组

文章数量:1302350

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

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

Share edited Jun 20, 2020 at 9:12 CommunityBot 11 silver badge asked Dec 11, 2016 at 15:45 Oki DokiOki Doki 1914 silver badges13 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 6

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. website, but there was no ID for email, only for password and login button and some other elements.

Load the web page in the WebView:

In your activity or fragment, load the web page containing the login form in the WebView. Make sure to enable JavaScript if the page requires it:

WebView webView = findViewById(R.id.webview);
webView.getSettings().setJavaScriptEnabled(true);
webView.loadUrl("https://example./login");

To autofill the form with the username and password, you can inject JavaScript into the WebView. You'll need to know the IDs or names of the input fields in the web page's HTML source. For example, if the input fields have the IDs username_input and password_input, you can use the following code:

String username = "your_username";
String password = "your_password";

String js = "javascript:(function() { " +
        "document.getElementById('username_input').value = '" + username + "';" +
        "document.getElementById('password_input').value = '" + password + "';" +
        "})()";

webView.setWebViewClient(new WebViewClient() {
    @Override
    public void onPageFinished(WebView view, String url) {
        super.onPageFinished(view, url);
        view.loadUrl(js);
    }
});

本文标签: javascriptHow can I autofill a websites fieldsforms in a WebView in Android StudioStack Overflow