Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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:

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

like image 367
Oscar Méndez Avatar asked Oct 15 '25 09:10

Oscar Méndez


1 Answers

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

like image 198
Rajesh Nasit Avatar answered Oct 16 '25 22:10

Rajesh Nasit



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!