Excel1 Excel1 - 10 days ago 5
Android Question

Website Login by using WebView Javascript Android

On Button click:

public void login(View v) {

InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(rlayout.getWindowToken(), 0);


loginWebView.loadUrl("https://example.de");
loginWebView.setVisibility(View.INVISIBLE);
loginWebView.setWebViewClient(new WebViewClient() {

public void onPageFinished(WebView view, String url) {
loginWebView.loadUrl("javascript: {" +
"document.getElementById('login_act').value = '" + "USNAME" + "';" +
"document.getElementById('login_pwd').value = '" + "PWD" + "';" +
"document.getElementById('submit').click();" +
"};");
}

public void onPageFinished2(WebView view, String url) {
loginWebView.loadUrl(url);
}
});

loginWebView.clearCache(true);
loginWebView.clearHistory();


WebView view = (WebView) this.findViewById(R.id.mainwebView);

String url = "https://example.de";
view.loadUrl(url);


}


On Button click:
1- 1. WebView Login
2- 2. WebView show Page
3- Ready

At my first try all worked. No Error, no problems. The only thing was, that the WebView loaded with the page, where i was not logged in.

How can i log in and what went wrong ?

Answer

Try to replace:

public void onPageFinished(WebView view, String url) { loginWebView.loadUrl("javascript: {" + "document.getElementById('login_act').value = '" + "USNAME" + "';" + "document.getElementById('login_pwd').value = '" + "PWD" + "';" + "document.getElementById('submit').click();" + "};"); }

With:

public void onPageFinished(WebView view, String url) { loginWebView.loadUrl("javascript:{" + "ins=document.getElementsByTagName('input');" + "ins[0].value='usrn';" + "ins[1].value='pwd';" + "ins[2].value=true;" + "document.getElementsByTagName('form')[0].submit();" + "};" ); }

Replace for usrn your username and replace for pwd your password. Use ins[2].value=true to hit "remember me". (If there no "remember me" checkbox delete it)

The last entry hit the submit Button.

Main Sourcecode by Jonas w - http://stackoverflow.com/users/5260024/jonas-w