Oscar Méndez Oscar Méndez - 2 years ago 272
Android Question

fill form programmatically in Android Webview - JavaScript

I'm trying to automatically fill a form from the website of my school.
I've seen some ways to do with javascrip.

Here is my code:

protected void onCreate(Bundle savedInstanceState) {

navegador_siiau = (WebView) findViewById(R.id.wv_navegador_siiau);
navegador_siiau.setWebViewClient(new WebViewClient() {
public void onPageFinished(WebView view, String url) {
String user="207512134";
String pwd="FENMAA";
view.loadUrl("javascript:document.getElementsByName('p_codigo_c').value = '"+user+"';document.getElementsByName('p_clave_c').value='"+pwd+"';");


The result is a blank page with the last string I try to put in the form...
What can I do to fill and submit the form correctly?

Answer Source

This is untested, but I see two things wrong here:

  1. You should call setWebViewClient with your WebViewClient implementation before you call loadUrl. However, loadUrl is asynchronous, so it probably would not make a difference, but this is worth a try I think.

  2. You are not calling super.onPageFinished(view, url); in onPageFinshed. You should add this call.


I finally took a look at the actual page you are working with. The problem is that the page loads content in a different frame, so you actually have to make the getElementsByName call on a different document. The frame in which both of the inputs you want are located has the name mainFrame in your page. So, you should write the javascript like this and load that into the WebView as you have done above:

                 getElementsByName('p_codigo_c')[0].value = "user";
                 getElementsByName('p_clave_c')[0].value = "password";
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download