0

I am stucked in one functionality. I am storing login credentials in my app for website. And there is one LOGIN button is there. When I hit that button, it will redirect to webview with website. And if there is any email and password, my login credentials will be filled there.

Example: I have stored facebook credentials in my app. And my webview is loading with https://m.facebook.com and when it will finish loading my stored login credentials will be filled there. So I just need to hit login button from webview to move forward.

For my app I am using following code. Which is not working for most website. Can anyone help me how to figure out this issue? Or does anyone has better idea to achieve this kind of functionality?

wvAutofill.loadUrl("https://mobile.twitter.com/login");
wvAutofill.setWebViewClient(new WebViewClient() {

    @Override
    public boolean shouldOverrideUrlLoading(WebView view, String url) {
        view.loadUrl(url);
        return true;
    }

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

        final String js = "javascript: var x = document.getElementsByTagName(\"form\")[0].getElementsByTagName('input');" +
                "for (var i = 0; i < x.length; i++) {" +
                "\nconsole.log(x[i].type);" +
                "var type = x[i].type;" +
                "if (type=='text'){" +
                "x[i].value=\"abc@gmail.com\";" +
                "};" +
                "if (type == 'password'){\n" +
                "x[i].value=\"abc123\";" +
                "}; " +
                "if (type == 'button' || type == 'submit' ||  document.getElementsByTagName(\"button\")[0].tagName == \"button\"){" +
                "x[i].click();" +
                "}; " +
                "}";

        view.loadUrl(js);
    }
});
  • By following this answer http://stackoverflow.com/a/24977839/3796784 you can achieve what you want. – Umesh Chauhan Feb 23 '17 at 11:10
  • @UmeshChauhan, this solution is works for only specific website. But here User will enter any website, consider he will enter http://www.amazon.in/. Then how can I achieve? Your solution is work for me if I know the id of specific input type. –  Feb 23 '17 at 11:15
  • I think it is bit difficult to make a generic solution as there is no standard for ids. Um.. may be you can look for text field and there type (text/password) and build a logic around it, but it is not a concrete solution to problem. – Umesh Chauhan Feb 23 '17 at 12:47

1 Answers1

0

To automatically click a search button in an Android WebView, you can inject JavaScript code after the page has finished loading. First, you need to know the ID or the class of the button in the web page's HTML source. For example, if the button has the ID next_button, you can use the following code:

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

String js = "javascript:(function() { " +
        "document.getElementById('next_button').click();" +
        "})()";

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

If the button has a class instead of an ID, you can modify the JavaScript code accordingly. For example, if the button has the class search_button, you can use this code:

String js = "javascript:(function() { " +
        "document.getElementsByClassName('next_button')[0].click();" +
        "})()";

This code assumes that the search button is the first element with the specified class. Adjust the index [0] if the button is not the first element with that class

Mori
  • 2,653
  • 18
  • 24