javascript – Fill fields in webview programmatically-ThrowExceptions

Exception or error:

I was trying to insert values into fields in a webpage loaded inside a webview .

The exact thing i was doing was this –

String date = "5/8/1994";
webview.loadUrl("javascript:var uselessvar_a =document.getElementById('dpStudentdob_dateInput').value='" + date + "';");

Problem

  • So this is suppose to insert the value into the field into the given
    id. and it gives me the exact result , it insert into the field. but
    when the submit button is pressed the error is shown as this data is
    not correct. but when i insert the values by typing the same value is
    accepted.
  • The problem is with inserting value into the DOB field in the
    webpage.
  • The registration number field works fine and the clicking button also
    works fine in my code.

the webpage is was loading is this.

I want to enter the date into the date DOB field in the webpage.

My full java class is-

public class MainActivity extends AppCompatActivity {

WebView webview;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);

    webview = (WebView) findViewById(R.id.webView);

    webview.getSettings().setBuiltInZoomControls(true);
    webview.getSettings().setDisplayZoomControls(false);

    webview.getSettings().setUseWideViewPort(true);
    webview.getSettings().setLoadWithOverviewMode(true);

    webview.getSettings().setJavaScriptEnabled(true);
    webview.setWebChromeClient(new WebChromeClient());

    webview.getSettings().setDomStorageEnabled(true);

    webview.setWebViewClient(new WebViewClient() {
        @Override
        public void onPageFinished(WebView view, String url) {
            super.onPageFinished(view, url);
            String password = "1201329022";
            String date = "5/8/1994";
            webview.loadUrl("javascript:var uselessvar =document.getElementById('txtRegNo').value='" + password + "';");
            webview.loadUrl("javascript:var uselessvar_a =document.getElementById('dpStudentdob_dateInput').value='" + date + "';");
            webview.loadUrl("javascript:var uselessvar_b =document.getElementById('btnView').click();");
        }
    });

    webview.loadUrl("http://bputexam.in/StudentSection/ResultPublished/StudentResult.aspx");
}

}

Do any one have any suggestion for this.

How to solve:

To insert text inside an input text field in webview you have to use javascript like below:

webView.loadUrl("javascript:(function(){l=document.getElementById('form1:txtDrname').value='" + editTextInput.getText().toString() + "';})()");

Then you can click a button inside webview to perform search:

webView.loadUrl("javascript:(function(){l=document.getElementById('form1:btnSubmit');e=document.createEvent('HTMLEvents');e.initEvent('click',true,true);l.dispatchEvent(e);})()");

Leave a Reply

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