fill form programmatically in Android Webview - JavaScript

后端 未结 4 923
灰色年华
灰色年华 2021-01-15 07:33

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
prote         


        
4条回答
  •  臣服心动
    2021-01-15 08:26

    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 arr = new ArrayList();
    arr.add("document.getElementsByName(\"tag_name\")[0].value  = \"your_value\";");
    .
    .
    .
    call setValue(arr, 0)
    
    private void setValue(final ArrayList arr, final int i) {
            if (i < arr.size()) {
                webView.evaluateJavascript(arr.get(i), new ValueCallback() {
                    @Override
                    public void onReceiveValue(String value) {
                        int j = i + 1;
                        setValue(arr, j);
                    }
                });
            }else{
                webView.evaluateJavascript("document.getElementById(\"your_form_id\").submit();", new ValueCallback() {
                    @Override
                    public void onReceiveValue(String value) {
                        Toast.makeText(MainActivity.this, "Submitted: "+value, Toast.LENGTH_SHORT).show();
                    }
                });
            }
        }
    

提交回复
热议问题