Q:

Javascript not always working in WebView

I want to open a webpage in a WebView and click automatically on a button after the page has loaded.  This is my current WebView class:
public class WebViewFragment extends Fragment {

    ...

    @Override
    public void onResume() {
        super.onResume();
        openUrl();
    }

    private void openUrl() {
        setCookies();
        if (urlToOpen ! = null) {
            //tell webview to handle redirects (by default browser launches on redirects)
            webview.setWebViewClient(new WebViewClient() {
                @Override
                public void onPageStarted(WebView view, String url, Bitmap favicon) {
                    super.onPageStarted(view, url, favicon);

                    if (getActivity() ! = null)
                        ((MainActivity) getActivity()).onShowLoading();
                }

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

                    if (getActivity() ! = null) {
                        executeJS();
                        ((MainActivity) getActivity()).onHideLoading();
                    }
                }
            });
            if (javascriptToExecute ! = null) {
                webview.getSettings().setJavaScriptEnabled(true);
            }
            webview.loadUrl(urlToOpen);
        }

    }

    private void executeJS() {
        System.out.println("executeJS(): " + javascriptToExecute);
        webview.loadUrl(javascriptToExecute);
    }    
}

I can use "javascript:$('#customer-info-edit').click();" or "javascript:document.getElementById('customer-info-edit').click();" both work fine.  
But the problem is they work only one time.  If I open the WebView for the first time, the button is clicked.  However if I press physical back button and open the WebView again then the button is not clicked.  Why isn't Javascript working every time? 

A:

I don't know what the problem exactly was, but I found a working solution: add 500ms delay before executing javascript. 