user1991
user1991

Reputation: 179

Autofill fields in web view android

I am creating an application which shows an external website in a webview. I want to fill the username and password fields of that web view with my custom values, but i am not finding any success to do so:

HERE IS THE WEBSITE PAGE SOURCE SNIPPET:

<form name="LoginForm" method="post" action="https://xyz" onsubmit="document.LoginForm.Submit.disabled=true;">

    <input type=hidden name="XXX" value="@@@@www@">
    <input type=hidden name="YYY" value="xyz.0">

            <tr>
            <td width="50%">Username
                <font class=starmand align="absmiddle">*</font></td>
            <td width="50%"><input type="text" name="userName" class="txtfld" autocomplete="off" size="12"/>
              </td>
          </tr>
            <tr>
            <td>Password
                <font class=starmand align="absmiddle">*</font></td>
            <td><input type="password" name="password" size="12" value="" class="txtfld">
              </td>
          </tr>
            <tr>
            <td align="right" colspan="2"><input type="submit" name="Submit" value="GO" onclick="return xyz();" class="btnSubmit"></td>

          </tr>
          </table></td>
  </table>
  </tbody>
</form>

And here is my Android Code Snippet:

 @Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    browser = (WebView)findViewById(R.id.webView1);
    browser.setWebViewClient(new MyBrowser());
    browser.getSettings().setLoadsImagesAutomatically(true);
    browser.getSettings().setJavaScriptEnabled(true);
    browser.setScrollBarStyle(View.SCROLLBARS_INSIDE_OVERLAY);
    browser.loadUrl(AppConstants.LOGIN_URL);
}


private class MyBrowser extends WebViewClient {

    @Override
    public void onPageFinished(WebView view, String url) {
        super.onPageFinished(view, url);
        String TAG="onPageFinished";
        Logger.i(TAG,"Called..");
        if(url.equals(AppConstants.LOGIN_URL)) {
            Logger.i(TAG,"Login page load done..");
            updateStatus("Login Page Loaded..!",TYPE_GREEN);
            browser.loadUrl(getLoginScript("USER_NAME","PASS_WORD"));
        }
    }

}

public static String getLoginScript(String userName,String password){
    return "javascript: {" +
            "document.getElementsByName('userName').value = '"+userName +"';" +
            "document.getElementsByName('password').value = '"+password+"'; };";
}

On the Running the above code:

-I get a new web Page with a text as USER_NAME

Please Help me!

I need to get this working...

Thanks in advance.

Upvotes: 0

Views: 1684

Answers (1)

Anthony
Anthony

Reputation: 21

It looks like you're loading it as a default of "USER_NAME". To me it would be make more sense if the field is populated with the text "USER_NAME" since you pass that string as a parameter like below:

browser.loadUrl(getLoginScript("USER_NAME","PASS_WORD"));

Upvotes: 2

Related Questions