document.getElementsByName在android webview中的某些网站中不起作用

时间:2019-04-15 10:07:14

标签: javascript android-webview

我构建了一个新的android应用程序,并使用Java脚本语法document.getElementsByName('')在网页的输入字段中插入值。但无法在我在android studio的网络视图中加载的某些网页上工作

此处是代码

 webCollection =(WebView)findViewById(R.id.webCollection);

 webCollection.getSettings().setJavaScriptEnabled(true);
 webCollection.getSettings().setDomStorageEnabled(true);



  webCollection.loadUrl("mywepSite");

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


  view.loadUrl("javascript:(function(){document.getElementsByName('j_username')[0].value='myUser';document.getElementsByName('j_password')[0].value='MyPassword';document.getElementsByTagName('form')[0]();})()");

                   }
               });

           }
       });

此处是输入字段

<input name="j_username" tabindex="1" class="form-control input-lg margin-bottom" id="j_username" spellcheck="false" type="text" size="15" autocapitalize="off" autocorrect="off" autocomplete="off" data-required="Please enter required fields.">

<input name="j_password" tabindex="1" class="form-control input-lg margin-bottom" id="j_password" spellcheck="false" type="text" size="15" autocapitalize="off" autocorrect="off" autocomplete="off" data-required="Please enter required fields.">

3 个答案:

答案 0 :(得分:0)

那是因为您正在访问内部类中的局部变量。只需将webCollection定为最终版本即可。

final WebView  webCollection =(WebView)findViewById(R.id.webCollection);

必须在内部类主体之前明确分配任何在内部类中使用但未声明的局部变量。

还要检查清单中的权限

<uses-permission android:name="android.permission.INTERNET" />

Java语言规范,https://docs.oracle.com/javase/specs/jls/se7/html/jls-8.html#jls-8.1.3

中的说明

答案 1 :(得分:0)

这是一个可以成功加载Web视图的工作类

import android.app.Activity;
import android.os.Bundle;
import android.webkit.WebResourceError;
import android.webkit.WebResourceRequest;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Toast;
import android.annotation.TargetApi;

public class Main extends Activity {

    private WebView mWebview ;

    @Override
    public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        mWebview  = new WebView(this);

        mWebview.getSettings().setJavaScriptEnabled(true); // enable javascript

        final Activity activity = this;

        mWebview.setWebViewClient(new WebViewClient() {
            @SuppressWarnings("deprecation")
            @Override
            public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
                Toast.makeText(activity, description, Toast.LENGTH_SHORT).show();
            }
            @TargetApi(android.os.Build.VERSION_CODES.M)
            @Override
            public void onReceivedError(WebView view, WebResourceRequest req, WebResourceError rerr) {
                // Redirect to deprecated method, so you can use it in all SDK versions
                onReceivedError(view, rerr.getErrorCode(), rerr.getDescription().toString(), req.getUrl().toString());
            }
        });

        mWebview .loadUrl("http://www.google.com");
        setContentView(mWebview );

    }

}

不要忘记添加权限

<uses-permission `android:name="android.permission.INTERNET" />

答案 2 :(得分:0)

webCollection.setWebViewClient(new WebViewClient() {
@Override
public void onLoadResource(WebView view, String url) {
super.onLoadResource(view, url);
view.loadUrl("javascript:(function(){document.getElementsByName('j_username') 
[0].value='myUser';document.getElementsByName('j_password') 
[0].value='myPassword';document.getElementsByTagName('form')[0]();})()");
}  
});