简体   繁体   English

使用Javascript在Webview(Android)中填写表单而没有输入类型的元素ID(文本和密码)?

[英]Using Javascript to fill form in webview(Android) without element ID of input type- text and password?

I have opened the login page of a website(Cyberoam-college) in webview of my app. 我已经在我的应用程序的webview中打开了一个网站(Cyber​​oam-college)的登录页面。 now I want to make it so that user won't have to type the username and password again and again, rather they can select it from a list or somewhere else and the app would automatically fill the username, password and click login for them. 现在,我想这样做,以便用户不必一次又一次地输入用户名和密码,而是可以从列表或其他地方选择它,该应用程序将自动填写用户名,密码并单击登录。 I've read that to do this I need to implement javascript in my webview. 我读过要做到这一点,我需要在Webview中实现javascript。

The Problem is, both username and password fields don't have id in their html tags, just the name attribute- 问题是,用户名和密码字段的html标记中都没有ID,只是名称属性-

<input type="text" name="username">
<input type="password" name="password">

The Login Button has ID, so it can be used- 登录按钮具有ID,因此可以使用-

<input type="submit" name="btnSubmit" value="Login" id="logincaption">

Now I somehow need to use strings stored in variables in MainActivity.java to fill first the username textField, then the password field and finally click the login button when user clicks the element in the list storing list of usernames and passwords. 现在,我需要以某种方式使用MainActivity.java中变量中存储的字符串来填充用户名textField,然后填充密码字段,最后,当用户单击存储用户名和密码列表的列表中的元素时,最后单击登录按钮。

Please tell me how to implement this in Android! 请告诉我如何在Android中实现此功能!

Thirst of all make sure that isn't iframe. 首先请确保不是iframe。 I believe there was no reason to add a webView for login purposes if you can build native login. 我相信,如果您可以构建本机登录,则没有理由添加WebView进行登录。 Anyway if you need to select inputs you can use: 无论如何,如果您需要选择输入,则可以使用:

  function login(){
        document.querySelector("input[name=username]").value  = "username";
        document.querySelector("input[name= password]").value  = "password";
        document.forms[0].submit();
 }

To pass values from Java code to JS you have to write a bridge and trigger JS function You can read how to build bridge here: http://blog.cuelogic.co.in/simple-android-java-javascript-bridge/ 要将值从Java代码传递给JS,您必须编写一个网桥并触发JS函数。您可以在此处阅读如何构建网桥: http : //blog.cuelogic.co.in/simple-android-java-javascript-bridge/

First, use the following statement 首先,使用以下语句

@SuppressLint("SetJavaScriptEnabled")

then, you could: 然后,您可以:

mWebView = (WebView) findViewById(R.id.webview); 
mWebView.getSettings().setJavaScriptEnabled(true);
mWebView.setWebChromeClient(new WebChromeClient());
mWebView.setWebViewClient(new WebViewClient(){
    @Override
    public void onPageFinished(WebView view, String url) {

        String javascript="javascript: document.getElementById('someid').innerHTML='Hello World!';";
        view.loadUrl(javascript);
    }
});
mWebView.loadUrl(URL);

You can insert Java interfaces using this method to control the asynchronously via Java methods. 您可以使用此方法插入Java接口,以通过Java方法异步控制。

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

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