MNM
MNM

Reputation: 2743

How to run a javascript on a webpage in a web view Android

I have a form that I want to fill in and submit automatically. I have tried with he using a WebviewClient and then waiting until the page is finished loading before I use the evaluateJavascript for a web view. It all looks right and my javascript seems to be valid per a validator online. So I don’t know where the issue is happing.

Here is what I have in the android app for my code:

 //Login infomration get from stored perferences.
String username = "[email protected]";
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);
        }
    });

}

And this is the html form that I am trying to access and change.

<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>

This is the script that I and sending via string.

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

Upvotes: 1

Views: 768

Answers (1)

Deyu瑜
Deyu瑜

Reputation: 544

try to add this line

webView.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);

Upvotes: 1

Related Questions