如何在web视图自动输入密码 [英] How to enter password automatically in Webview

查看:506
本文介绍了如何在web视图自动输入密码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是新来的Java和Android和我有一个问题。 我认为,解决的办法是,也许很容易,但我不可能解决这个问题。

我创建一个web视图的事情是我想要的web视图自动进入用户名和密码,以便已经登录的用户,当他启动应用程序中的应用程序。 我GOOGLE了一下,发现类似的东西,但它不能正常工作

  @覆盖
保护无效的onCreate(包savedInstanceState){
    super.onCreate(savedInstanceState);
    的setContentView(R.layout.activity_main);
    web视图=(web视图)findViewById(R.id.webView1);
    webView.getSettings()setJavaScriptEnabled(真)。
    webView.loadUrl(https://euterpe.webuntis.com/WebUntis/index.do#main);


webView.setWebViewClient(新WebViewClient());



类MyWebViewClient扩展WebViewClient {
@覆盖
公共无效onReceivedHttpAuthRequest(web视图来看,
        HttpAuthHandler处理器,主机字符串,字符串的境界){

    handler.proceed(用户名,密码);

}
}
 

解决方案

尽管这是一个老问题,我想我会后我的解决办法:

  webView.getSettings()setJavaScriptEnabled(真)。

    webView.loadUrl(HTTP://your.url);

    webView.setWebViewClient(新WebViewClient(){

        公共无效onPageFinished(web视图查看,字符串URL){
            view.loadUrl(JavaScript的:document.getElementsByName('学校')[0] .value的='schoolname');
            view.loadUrl(JavaScript的:document.getElementsByName('为j_username')[0] .value的='用户名');
            view.loadUrl(JavaScript的:document.getElementsByName('为j_password')[0] .value的='密码');

            view.loadUrl(JavaScript的:document.forms ['登录']提交()。);
        }
    });
 

它看起来相像从本杰明答案。唯一的区别是在 [0] 部分,选择第一个找到的元素,再加上我执行等待页面,它firering的JavaScript之前加载。

I am new to Java and Android and I got a question. I think the solution is maybe very easy but I couldn't solve it.

I created an application with a WebView the thing is I want that the webview enters username and password automatically so that the user is already logged in when he starts the application. I googled it and found something like that but it does not work

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    webView = (WebView) findViewById(R.id.webView1);
    webView.getSettings().setJavaScriptEnabled(true);
    webView.loadUrl("https://euterpe.webuntis.com/WebUntis/index.do#main");


webView.setWebViewClient(new WebViewClient ());



class MyWebViewClient extends WebViewClient {
@Override
public void onReceivedHttpAuthRequest(WebView view,
        HttpAuthHandler handler, String host, String realm) {

    handler.proceed("Username", "password");

}
}

解决方案

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.

这篇关于如何在web视图自动输入密码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆