How to enter password automatically in Webview

我与影子孤独终老i 提交于 2019-12-05 12:35:01

hello please try this:

webView.getSettings().setJavaScriptEnabled(true);

webView.loadUrl("javascript:document.getElementsByName('school').value = 'schoolname'");
webView.loadUrl("javascript:document.getElementsByName('j_username').value = 'username'");
webView.loadUrl("javascript:document.getElementsByName('j_password').value = 'password'");

webView.loadUrl("javascript:document.forms['login'].submit()");

the first line enables javascript

all other lines fill in the form and eventually submit the form.

Look at the String apostrophes please i edited my answer.

Though this is an old question I thought I would post my solution:

    webView.getSettings().setJavaScriptEnabled(true);   

    webView.loadUrl("http://your.url");

    webView.setWebViewClient(new WebViewClient() {

        public void onPageFinished(WebView view, String url) {
            view.loadUrl("javascript:document.getElementsByName('school')[0].value = 'schoolname'");
            view.loadUrl("javascript:document.getElementsByName('j_username')[0].value = 'username'");
            view.loadUrl("javascript:document.getElementsByName('j_password')[0].value = 'password'");

            view.loadUrl("javascript:document.forms['login'].submit()");
        }
    });

It looks much alike the answer from Benjamin. Only difference is the [0] part, selecting the first found element, plus my implementation waits for the page to load before firering javascript at it.

Replace

webView.setWebViewClient(new WebViewClient ());

with

webView.setWebViewClient(new MyWebViewClient ());

Only then the override method onReceivedHttpAuthRequest will get called.

This handling will only work for HTTP authentication requests.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!