使用 Javascript 在 WebView 中填写表单

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10187908/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 08:58:11  来源:igfitidea点击:

Fill form in WebView with Javascript

javascriptandroidandroid-webview

提问by Jay

I'm trying to fill Webforms from a Webview in Android. I've already found this piece of code here: Fill fields in webview automatically

我正在尝试从 Android 中的 Webview 填充 Webforms。我已经在这里找到了这段代码:自动填充 webview 中的字段

String username = "cristian";
webview.loadUrl("javascript:document.getElementById('username').value = '"+username+"';");

Unfortunatly I dont understand where I have to open the page I want to fill in.

不幸的是,我不明白我必须在哪里打开我想要填写的页面。

setContentView(R.layout.web);
final WebView mWebView = (WebView) findViewById(R.id.webview);
mWebView.getSettings().setJavaScriptEnabled(true);
mWebView.loadUrl(url);
String user="u";
String pwd="p";
mWebView.loadUrl("javascript:document.getElementById('username').value = '"+user+"';document.getElementById('password').value='"+pwd+"';");

When I try it this way, the site gets displayed but without any values in the forms.

当我以这种方式尝试时,该站点会显示,但表单中没有任何值。

Thanks in advance for helping

预先感谢您的帮助

回答by gnpaolo

You should fill the values after the page has been loaded. This is an example using your code:

您应该在页面加载后填写这些值。这是使用您的代码的示例:

mWebView.loadUrl(url);
mWebView.getSettings().setJavaScriptEnabled(true);
mWebView.setWebViewClient(new WebViewClient() {
    public void onPageFinished(WebView view, String url) {
        String user="u";
        String pwd="p";
        view.loadUrl("javascript:document.getElementById('username').value = '"+user+"';document.getElementById('password').value='"+pwd+"';");
    }
});

回答by awind

Android 4.4 new WebView have the issue with use loadUrl("javascript:")method, the paramter string will be url-decode before execution. You can try use evaluateJavascript()for API >= 19 and loadUrl()for API < 19. The code below is work for me.

Android 4.4 新的 WebView 有使用loadUrl("javascript:")方法的问题,参数字符串将在执行前进行 url-decode。您可以尝试使用evaluateJavascript()API >= 19 和loadUrl()API < 19。下面的代码对我有用。

    mWebView.loadUrl(url)
    WebSettings settings = mWebView.getSettings();
    settings.setJavaScriptEnabled(true);

    String js = "javascript:document.getElementById('username').value = '"+user+"';document.getElementById('password').value='"+pwd+"';";
    mWebView.setWebViewClient(new WebViewClient() {

        @Override
        public void onPageFinished(WebView view, String url) {
            super.onPageFinished(view, url);

            if (Build.VERSION.SDK_INT >= 19) {
                view.evaluateJavascript(js, new ValueCallback<String>() {
                    @Override
                    public void onReceiveValue(String s) {

                    }
                });
            } else {
                view.loadUrl(js);
            }
       });
    }

please refer: loadUrl("javascript:....") behaviour changed incompatibly in Android 4.4

请参考:loadUrl("javascript:....") 行为在 Android 4.4 中发生了不兼容的变化

回答by Denish Rana

Just Enable DomStorage and write "var x=" to string:

只需启用 DomStorage 并将“var x=”写入字符串:

webview.getSettings().setJavaScriptEnabled(true);
web.getSettings().setDomStorageEnabled(true);

webview.loadUrl(urlString);
webview.setWebViewClient(new WebViewClient(){

public void onPageFinished(WebView view, String url){  
super.onPageFinished(view, url);
String js = "javascript:var x=document.getElementById('username').value = '"+user+"';var y=document.getElementById('password').value='"+pass+"';";

if (Build.VERSION.SDK_INT >= 19) {
        view.evaluateJavascript(js, new ValueCallback<String>() {
            @Override
            public void onReceiveValue(String s) {

            }
        });
    } else {
        view.loadUrl(js);
    }

view.loadUrl(js);
}
});

回答by Edess Elder

I had the same issue, and after following different explanations on stackOverflow, I succeed to make mine working.

我遇到了同样的问题,在对 stackOverflow 进行了不同的解释之后,我成功地让我的工作正常工作。

here is my approach

这是我的方法

webView.loadUrl(url);
        webView.getSettings().setDomStorageEnabled(true);
        webView.setWebViewClient(new WebViewClient(){
            public void onPageFinished(WebView view, String url) {
                String email="[email protected]";

                //view.loadUrl("javascript:document.getElementById('email').value = '"+email+"'");
                view.loadUrl("javascript:document.forms[0].email.value = '"+email+"';");
                Log.d("email", "can not add email");
            }
        });

2 things : 1) you need to add this line webView.getSettings().setDomStorageEnabled(true);(reference: Android WebView always returns null for javascript getElementById on loadUrl)

2 件事:1)你需要添加这一行webView.getSettings().setDomStorageEnabled(true);(参考:Android WebView 总是为 loadUrl 上的 javascript getElementById 返回 null

2) you need to access the variable in your php code by using this view.loadUrl("javascript:document.forms[0].email.value = '"+email+"';");as you can see in my code I used the solution proposed by @gnpaolo, but it didn't work for me so I commented it and use this one. (reference: How to inject a String into Android WebView)

2)您需要使用它来访问php代码中的变量,view.loadUrl("javascript:document.forms[0].email.value = '"+email+"';");正如您在我的代码中看到的那样,我使用了@gnpaolo提出的解决方案,但它对我不起作用,所以我对其进行了评论并使用了这个。(参考:如何将字符串注入 Android WebView

Finally, just want to add that you do not need to create a special javascript.

最后,只想补充一点,您不需要创建特殊的 javascript。

one more thing the forms[0]is the position of the variable in the php form, and in my case, I have the email of the user, so I wrote view.loadUrl("javascript:document.forms[0].email.value = '"+email+"';");

还有一件事forms[0]是变量在php表单中的位置,就我而言,我有用户的电子邮件,所以我写了view.loadUrl("javascript:document.forms[0].email.value = '"+email+"';");

Hope, this can help others.

希望,这可以帮助其他人。