Fill website data and click button and parse response

Akshat :

I want to let users enter the vehicle number and then read the data and show the vehicle details to the user. I don't want to do it in a webview. I am able to fill the data using this code:

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 + "';" +
                             "var frms = document.getElementsByName('convVeh_Form');" +
                             "frms[0].submit(); };");

                     isFirstLoad = false;
                 }
            }
        });

Here is the website which shows the data for this app.

https://parivahan.gov.in/rcdlstatus/vahan/rcstatus.xhtml

Now I am trying to click the submit button using this line

 "frms[0].submit(); };");

and this,

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

but they are not working. How to click the button whose Id is

convVeh_Form:j_idt21

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.?

TylerY86 :

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.

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=457509&siteId=1