简体   繁体   English

以编程方式填写 webview 中的字段

[英]Fill fields in webview programmatically

I was trying to insert values into fields in a webpage loaded inside a webview.我试图将值插入到 webview 中加载的网页的字段中。

The exact thing i was doing was this -我正在做的确切事情是这个 -

String date = "5/8/1994";
webview.loadUrl("javascript:var uselessvar_a =document.getElementById('dpStudentdob_dateInput').value='" + date + "';");

Problem问题

  • So this is suppose to insert the value into the field into the given id.所以这是假设将值插入字段到给定的 id 中。 and it gives me the exact result, it insert into the field.它给了我确切的结果,它插入到字段中。 but when the submit button is pressed the error is shown as this data is not correct.但是当按下提交按钮时,会显示错误,因为此数据不正确。 but when i insert the values by typing the same value is accepted.但是当我通过键入相同的值来插入值时被接受。
  • The problem is with inserting value into the DOB field in the webpage.问题在于将值插入网页的 DOB 字段。
  • The registration number field works fine and the clicking button also works fine in my code.注册号字段工作正常,单击按钮在我的代码中也工作正常。

the webpage is was loading is this .正在加载的网页是这个

I want to enter the date into the date DOB field in the webpage.我想在网页的日期 DOB 字段中输入日期。

My full java class is-我的完整 java 类是-

public class MainActivity extends AppCompatActivity {

WebView webview;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);

    webview = (WebView) findViewById(R.id.webView);

    webview.getSettings().setBuiltInZoomControls(true);
    webview.getSettings().setDisplayZoomControls(false);

    webview.getSettings().setUseWideViewPort(true);
    webview.getSettings().setLoadWithOverviewMode(true);

    webview.getSettings().setJavaScriptEnabled(true);
    webview.setWebChromeClient(new WebChromeClient());

    webview.getSettings().setDomStorageEnabled(true);

    webview.setWebViewClient(new WebViewClient() {
        @Override
        public void onPageFinished(WebView view, String url) {
            super.onPageFinished(view, url);
            String password = "1201329022";
            String date = "5/8/1994";
            webview.loadUrl("javascript:var uselessvar =document.getElementById('txtRegNo').value='" + password + "';");
            webview.loadUrl("javascript:var uselessvar_a =document.getElementById('dpStudentdob_dateInput').value='" + date + "';");
            webview.loadUrl("javascript:var uselessvar_b =document.getElementById('btnView').click();");
        }
    });

    webview.loadUrl("http://bputexam.in/StudentSection/ResultPublished/StudentResult.aspx");
}

}

Do any one have any suggestion for this.有没有人对此有任何建议。

To insert text inside an input text field in webview you have to use javascript like below:要在 webview 中的输入文本字段中插入文本,您必须使用如下的 javascript:

webView.loadUrl("javascript:(function(){l=document.getElementById('form1:txtDrname').value='" + editTextInput.getText().toString() + "';})()");

Then you can click a button inside webview to perform search:然后你可以点击 webview 中的一个按钮来执行搜索:

webView.loadUrl("javascript:(function(){l=document.getElementById('form1:btnSubmit');e=document.createEvent('HTMLEvents');e.initEvent('click',true,true);l.dispatchEvent(e);})()");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM