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?

4 Answers 4

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

Sign up to request clarification or add additional context in comments.

2 Comments

Its works for me but I have one question. How to pass static String in value?
Please look into this sample codexpedia.com/android/…
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";

3 Comments

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
@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.
Thanks for take you the time to check the webpage and help me with this, that works!
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

Comments

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();
                }
            });
        }
    }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.