Excel1 Excel1 - 2 months ago 8
Android Question

Android: Login to a Website by using WebView

I tried to log me programmatically in a website: https://lmg-nhs.de/idesk/index.php

On Button click:

public void login(View v) {

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


loginWebView.loadUrl("https://lmg-nhs.de/index.php");
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://lmg-nhs.de/idesk/";
view.loadUrl(url);


}


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

Login to a website programmatically using JavaScript in Android application

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.

The Mainquestion is now: how to do it and what went here wrong ?

If you can, try to explain it at an or my example, because beginners (like me) can easier learn it this way.

Answer

(Just an advanced comment):

"javascript:{
 ins=document.getElementsByTagName('input');
ins[0].value='username';
ins[1].value='password';
ins[2].value=true;
document.getElementsByTagName('form')[0].submit();
}"

Das schreibt username ins erste, passwort ins zweite und setzt das Dritte auf true und schickt das erste formular ab...