如何在网页视图Android中的网页上运行JavaScript

时间:2016-11-07 02:50:44

标签: javascript android html webview webviewclient

我有一个表格,我想填写并自动提交。我尝试过他使用WebviewClient然后等到页面加载完毕后再使用evaluateJavascript进行Web视图。一切看起来都正确,我的javascript似乎是有效的在线验证器。所以我不知道问题出在哪里。

以下是我的Android应用程序中的代码:

 //Login infomration get from stored perferences.
String username = "Mil.G@gmail.com";
String password = "G12345!";
String javaScriptToLoadEmail = "document.getElementById(\"email\").value = \"" + username + "\";";
String javaScriptToLoadPass = "document.getElementById(\"pass\").value = \"" + password + "\";";
String javaScriptForm = "var frms = document.getElementsByName(\"send2\");";
String javaScriptSubmit = "frms[0].submit();";
String superJavaScript = "<script> " +
        javaScriptToLoadEmail +
        javaScriptToLoadPass  +
        javaScriptForm        +
        javaScriptSubmit +
        " </script>";
WebView webView;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    Log.d("Java Script", superJavaScript);

    webView = (WebView)findViewById(R.id.webview);
    webView.getSettings().setJavaScriptEnabled(true);
    webView.getSettings().setDomStorageEnabled(true);
    webView.loadUrl(LoginPage);
    webView.setWebViewClient(new WebViewClient()
    {
        @Override
        public void onPageStarted(WebView view, String url, Bitmap facIcon) {
            Log.d("Page is: ", "Loading");
            //SHOW LOADING IF IT ISNT ALREADY VISIBLE
        }

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

            Log.d("Page is:" , "Loaded");
            Log.d("Java Script", superJavaScript);
            webView.evaluateJavascript(superJavaScript, null);
        }
    });

}

这是我试图访问和更改的html表单。

<form action=“//…//“ method="post" id="login-form">

        <div class="col-2 registered-users">
            <div class="content">
                <h2><font><font>Customers who have already your registered</font></font></h2>
                <p><font><font>If you have an account, please login.</font></font></p>
                <ul class="form-list">
                    <li>
                        <label for="email" class="required"><em><font><font>*</font></font></em><font><font> E-mail address</font></font></label>
                        <div class="input-box">
                            <input type="text" name="login[username]" value="" id="email" class="input-text required-entry validate-email" title="mail address">
                        </div>
                    </li>
                    <li>
                        <label for="pass" class="required"><em><font><font>*</font></font></em><font><font> Password</font></font></label>
                        <div class="input-box">
                            <input type="password" name="login[password]" class="input-text required-entry validate-password" id="pass" title="password">
                        </div>
                    </li>

                </ul>



        <div class="col-2 registered-users">
            <div class="buttons-set">
                <button type="submit" class="button f-right" title="Login" name="send" id="send2"><span><span><font><font>Login</font></font></span></span></button>

            </div>
        </div>
    </div>
        </form>

这是我通过字符串发送的脚本。

<script> document.getElementById("email").value = "Mil.G@gmail.com";document.getElementById("pass").value = "G12345!";var frms = document.getElementsByName("send2");frms[0].submit(); </script>

1 个答案:

答案 0 :(得分:1)

尝试添加此行

webView.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);