Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Fill website data and click button and parse response

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

like image 444
Akshat Avatar asked Sep 26 '16 11:09

Akshat


2 Answers

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.

like image 191
TylerY86 Avatar answered Oct 29 '22 23:10

TylerY86


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.

like image 36
Johnny Cosmic Avatar answered Oct 29 '22 22:10

Johnny Cosmic