Multiple autoclicks in webview; Automate WebView in android to insert JavaScript one after another-ThrowExceptions

Exception or error:

What I have observed till now is that background tasks like the following code are executed after the whole main_activity.java has been run.

First I run this code.

webView = findViewById(R.id.webView);
webView.getSettings().setJavaScriptEnabled(true);
webView.setWebViewClient(new WebViewClient());
webView.loadUrl(initialTry);
webView.setWebChromeClient(new WebChromeClient() {
    public void onProgressChanged(WebView view, int progress) {
        if (progress == 100) {
            webView.loadUrl("javascript:document.getElementsByClassName('some-button-classname')[0].click()");
            Log.i("string1", "inside progress function");
        }
    }
});

Here I load a URL in the webview, Wait for it to finish loading (progress==100), then click a button in the URL by injecting JavaScript.
Now after clicking the button, the webpage changes (but has the same URL) and gives me another button which I want to press.

So I again do –

webView.loadUrl("javascript:document.getElementsByClassName('some-other-button-classname')[0].click()");
Log.i("string2", "after the progress function has finished");

But it doesn’t work.

I logged out some random string to check which string is printed first and saw that

string2 is printed first and after that the app tries to load the URL and then string1 is printed.

I want to click many buttons one after the other and the webview will change it’s HTML in the course.

How can I continue the process of clicking different buttons and not single one.

Basically – webpage loads – I click button – I wait – webpage changes – I click another button and so on…

How to solve:

Leave a Reply

Your email address will not be published. Required fields are marked *