Fill website data and click button and parse response

As I figured out correct id is convVeh_Form:j_idt26 instead of

"javascript:(function(){document.getElementById('convVeh_Form:j_idt21').click();})()"

Please, check my link to prove it

So, my main suggestion is to test your JS code in browser before adding it to Android. Debug mode in Chrome is really good for that, I am sure you know how to enable it.

After that if you sure that JS code works well, please add code to Android by logical blocks. That can help you to debug it.

For example first of all put following lines of the JS code:

document.getElementById('convVeh_Form:tf_reg_no1').value = 'text' document.getElementById('convVeh_Form:tf_reg_no2').value = 'text'

If you saw the result on the device you should add click event then.

document.getElementById('convVeh_Form:j_idt26').click()

It can separate your JS logic and you will be sure what actually wrong with code.

Regarding to this line:

Also, once able to click the button, the response will come from the website. How to read that response text and put it in textview of app.?

You should implement own WebViewClient() and overrideonPageFinishedand run JS code only on the correct page.

@Override
public void onPageFinished(WebView view, String url) {
    super.onPageFinished(view, url);
    if("https://parivahan.gov.in/rcdlstatus/vahan/rcstatus.xhtml".equals(url)) {
        // run JS
    } else if("response_link".equals(url)) {
        // notify user or any other logic
    }
}

Good luck!

P.S. All JS code above works well in browser on my side.


There's a separate problem you're having, convVeh_Form:j_idt21 is just a label (at the time of writing). You want convVeh_Form:j_idt27. Maybe they changed?

You may want to style your injected javascript and selector as document.querySelector('[id="convVeh_Form:j_idt27"]').click().

This gets around the : being a special character for purposes of a css selector.

webView = (WebView) findViewById(R.id.webView1);
webView.getSettings().setJavaScriptEnabled(true);
webView.loadUrl("https://parivahan.gov.in/rcdlstatus/vahan/rcstatus.xhtml");
webView.setWebViewClient(new WebViewClient() {
  public void onPageFinished(WebView view, String url) {
    String reg1 = "KA51X";
    String reg2 = "2442";
    if (isFirstLoad) {
      webView.loadUrl("javascript: {" +
        "document.getElementById('convVeh_Form:tf_reg_no1').value = '" + reg1 + "';" +
        "document.getElementById('convVeh_Form:tf_reg_no2').value = '" + reg2 + "';" +
        "document.querySelector('[id=\"convVeh_Form:j_idt27\"]').click(); };");

      isFirstLoad = false;
    }
  }
});

You could also use button[type="submit"] as the selector if you know it'll always be the first button, such that the Ids no longer matter. (Maybe they will change again?)
The other fields would be document.querySelectorAll('input[type="text"]')[0] and [1].

Once the form is submitted, the navigation will change. This triggers an event you can hook by subclassing the WebView. See this answer for the other question for an example.

private class HelloWebViewClient extends WebViewClient {
    @Override
    public boolean shouldOverrideUrlLoading(WebView view, String url) {
        // ...
    }
}

Use that as your event, detect the URL at the endpoint, and then inject some new javascript to extract the information from the page. Let me know if you need more help.