1

I have a very simple use case i.e to programmatically call a button of a web page loaded inside my webview. Below is the code

webView.getSettings().setDomStorageEnabled(true);
        webView.getSettings().setAppCacheEnabled(false);
        webView.getSettings().setCacheMode(WebSettings.LOAD_NO_CACHE);
        webView.getSettings().setJavaScriptEnabled(true);
        webView.setWebViewClient(new WebViewClient(){


            @Override
            public void onPageFinished(WebView view, String url) {
                //super.onPageFinished(view, url);
                switch (view.getOriginalUrl()){
                    case Properties.LOGIN_URL:

                        webView.evaluateJavascript(
                                "javascript:" +
                                        "var usernameField = document.getElementById('loginUserName');" +
                                        "usernameField.value = '" + Properties.USERNAME + "';" +
                                        "var passwordField = document.getElementById('loginPassword');" +
                                        "passwordField.value = '" + Properties.PASSWORD + "';", null
                        );

                        webView.evaluateJavascript(
                                "javascript:" +
                                        "document.getElementById('loginButton').click();", new ValueCallback<String>() {
                                    @Override
                                    public void onReceiveValue(String s) {

                                    }
                                }
                        );

                    break;
                }
            }
        });

view.loadUrl(Properties.LOGIN_URL);

The username and password field injection is working fine but the code to perform button click isn't working. I have searched a lot and experimented a lot but no luck, Is this even possible?

EDIT: The URL I'm accessing is private to our organization, is on http and not secured, not sure if it's relevant.

I have tried another public page with login fields and the same code works and the button gets clicked successfully.

2 Answers 2

1

Okay I found the reason. So the onPageFinished was getting called while all the DOM elements are not loaded hence the javascript was failing. For time being it to work I have added a delayed Handler with 5 sec delay and it worked. The right solution should be to check for the page to complete to load (may be use jquery to check when the document is ready).

Sign up to request clarification or add additional context in comments.

Comments

0

You can handle the webview button click this way

@JavascriptInterface
public void handleClick(String id) {
    startActivity(new Intent(mContext, MainActivity.class));
}

In javascript

Android.handleClick(_id);

1 Comment

Hi thanks for the answer. You have misunderstood the question. I want to click webview elements from the client side (android).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.