4

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:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.navegador_siiau_layout);

    navegador_siiau = (WebView) findViewById(R.id.wv_navegador_siiau);
    navegador_siiau.getSettings().setJavaScriptEnabled(true);
    navegador_siiau.loadUrl("http://siiauescolar.siiau.udg.mx/wus/gupprincipal.inicio");
    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?

Oscar Méndez
  • 937
  • 2
  • 13
  • 39

4 Answers4

5

this works for me for API version greater than 18

 mWebView = findViewById(R.id.web_view);   
        String url = "https://duckduckgo.com";

        mWebView.loadUrl(url);  
        mWebView.getSettings().setJavaScriptEnabled(true);

        final String js = "javascript:document.getElementById('search_form_input_homepage').value='android';" +
                "document.getElementById('search_button_homepage').click()";

        mWebView.setWebViewClient(new WebViewClient(){

            public void onPageFinished(WebView view, String url){
                if(Build.VERSION.SDK_INT >= 19){
                    view.evaluateJavascript(js, new ValueCallback<String>() {
                        @Override
                        public void onReceiveValue(String s) {
                        }
                    });
                }
            }
        });

here important is view.evaluateJavascript

Rajesh N
  • 6,198
  • 2
  • 47
  • 58
3

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.

EDIT 2:

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:

window.frames["mainFrame"].document.
                 getElementsByName('p_codigo_c')[0].value = "user";
window.frames["mainFrame"].document.
                 getElementsByName('p_clave_c')[0].value = "password";
mattgmg1990
  • 5,576
  • 4
  • 21
  • 26
  • When I use this: document.getElementsByName('p_codigo_c')[0].value nothing happend, but when I use: document.getElementsByName('p_codigo_c').value a new webpage is open with a String I put as value – Oscar Méndez Jul 27 '14 at 05:04
  • @OscarMéndez I took a look at the page. The problem is that you're not searching within the frame. I have edited my answer. I tested this solution and it works in a desktop web browser. I think it will work in WebView also. – mattgmg1990 Jul 27 '14 at 18:28
  • Thanks for take you the time to check the webpage and help me with this, that works! – Oscar Méndez Jul 28 '14 at 04:09
0

You just need to enable domelements

      myview.getSettings().setDomStorageEnabled(true);

and then use getelementby id like

     javascript:var x = document.getElementById('myfield').value = 'aaa';

and do this in

         onPageFinishedLoading(){}

got this from the link Android WebView always returns null for javascript getElementById on loadUrl

Community
  • 1
  • 1
Nasz Njoka Sr.
  • 1,138
  • 16
  • 27
0

For me, it is required to fill multiple entries in form one by one on callback of each "webView.evaluateJavascript" Note: API version > 18

See below snippet for reference

ArrayList<String> arr = new ArrayList<String>();
arr.add("document.getElementsByName(\"tag_name\")[0].value  = \"your_value\";");
.
.
.
call setValue(arr, 0)

private void setValue(final ArrayList<String> arr, final int i) {
        if (i < arr.size()) {
            webView.evaluateJavascript(arr.get(i), new ValueCallback<String>() {
                @Override
                public void onReceiveValue(String value) {
                    int j = i + 1;
                    setValue(arr, j);
                }
            });
        }else{
            webView.evaluateJavascript("document.getElementById(\"your_form_id\").submit();", new ValueCallback<String>() {
                @Override
                public void onReceiveValue(String value) {
                    Toast.makeText(MainActivity.this, "Submitted: "+value, Toast.LENGTH_SHORT).show();
                }
            });
        }
    }
Nitin
  • 1
  • 1