# Login mit HtmlUnit



## moccajoghurt (12. Nov 2010)

Hi,
ich möchte mich mit HtmlUnit auf einer Homepage einloggen. Versuche nun ersteinmal die form für den login zu finden.
Mit website.getForms(); habe ich die richtigen gefunden, allerdings weiß ich nicht welchen namen sie besitzen... folgende sind auf der Homepage enthalten:

```
[HtmlForm[<form method="get" action="/gallery/search/" id="search">], HtmlForm[<form action="/auth/loginSubmit" method="post" id="form-login">], HtmlForm[<form action="/auth/signup" method="post" id="form-signup-sidebar">]]
```

Welchen Namen besitzen jetzt die einzelnen forms? Bzw welchen String muss ich bei 
	
	
	
	





```
HtmlForm form = page1.getFormByName("");
```
 angeben?


----------



## JohannisderKaeufer (12. Nov 2010)

```
for(HtmlForm form : page.getForms){
System.out.println("Form Name: "+form.getNameAttribute());
}
```

Sowas sollte dir die Namen liefern, wenn Namen vergeben sind.

Ansonsten

```
HtmlForm form
for(HtmlForm f : page.getForms){
if("/auth/loginSubmit".equals(f.getActionAttribute())){
form =  f;
break;
}
}
```

oder schnöde sowas


```
HtmlForm form = (HtmlForm) page.getElementById("form-login");
```


----------



## moccajoghurt (12. Nov 2010)

Danke, das hat mir weitergeholfen, leider bin ich direkt schon auf das nächste Problem gestoßen. Der Login-Button ist vom typ "image" und hat weder eine id, noch einen Namen. Wie kann ich ihn jetzt klicken?

```
<input type="image" class="button login" src="/img/button_login.png" />
```


----------



## gman (12. Nov 2010)

Hi,

versuche mal:


```
final HtmlImageInput input = page1.getFirstByXPath ("//input[@type='image']");
input.click();
```

Quelle


----------



## JohannisderKaeufer (12. Nov 2010)

```
Form form;
((HtmlImageInput)form.getElementsByAttribute("input", "class","button login").get(0)).click();
```

Das Form hat man ja bereits identifiziert, dann kann man auch innerhalb des Forms nach seinem Button suchen. Anstatt XPath kann man auch die Vordefinierten Methoden verwenden. z.B. getElementsByAttribute.

Anstatt class und button login kann man auch auf ein anderes Attribute gehen.
z.B. type und image, src und den Pfad.


----------



## moccajoghurt (13. Nov 2010)

Vielen Dank für die Hilfe, klappt jetzt alles so wie ich es möchte!
Gruß mocca


----------



## Jigga (14. Nov 2010)

hallo,ich möchte mich ebenfalls mit hilfe von htmlunit auf einer seite einloggen.
jedoch besteht das "login feld" hier aus java script.scheinbar sind dir forms im script selbst enthalten.wie komme ich an diese denn jetzt am besten ran bzw wie setze ich meinen namen und mein password rein?

der ausschnitt des quellcodes um den es sich handelt:


```
<script type="text/javascript">
	var loading_holder = document.getElementById("member_panel");
	var ajax_loggedin = false;
	var login_form_lock = false;
	loading_holder.innerHTML = '<p class="logout"><img src="http://cdn.garenanow.com/webmain/static/images/ico_01.png" width="18" height="18" alt="" /><img src="http://cdn.garenanow.com/webmain/static/images/ajax_loader.gif" width="18" height="18" alt="Loadding...Please wait a minute" /></p>';
	
        function perform_login() {
            
        }
        
	function validateLogin(){
		//alert('validate');
		//var formLogin = document.getElementById("frmLogin");
		var username = document.frmLogin.vb_login_username.value;
		var password = document.frmLogin.vb_login_password.value;
		if (username == "" || password == "" || username == "Enter username here ..." || password == "1234567890") {
		      alert('Please enter both user name and password!');
		      return false;
		}
		perform_login();
		return true;
	}
	
	function keyIsPressed(evt) {
	      //alert('key pressed!');
	      var charCode = (evt.which) ? evt.which : evt.keyCode
	      if( charCode == 13 ) {
	            validateLogin();
	      }
	      return true;
	}
	
	
	function loggedin(username, shell, GP) {
		var member_panel = document.getElementById("member_panel");
		member_panel.innerHTML = '<p class="logout"><img src="http://cdn.garenanow.com/webmain/static/images/ico_01.png" width="18" height="18" alt="" /><span>'+username+'</span>[<a href="http://account.garena.com" title="edit">edit</a>][<a href="#" onClick="if(confirm(\'Sure to log out?\')) logout();" title="logout">logout</a>]<strong>Shells</strong>:'+number_format(shell);//+'<strong>EXP</strong>: '+number_format(GP)+'</p>';	
		ajax_loggedin = true;
	}


	<!-- Begin of check password -->
	function onLoginPasswordFocus(){
	  document.frmLogin.vb_login_password.value="";
	  document.frmLogin.passwordLabel.style.display = "none";
	  document.frmLogin.vb_login_password.style.display = "";
	  document.frmLogin.vb_login_password.focus();
	}
	
	function check_login_password(){
	  var password = document.frmLogin.vb_login_password.value;
	  login_form_lock = false;
  	  if(password == ""){
		document.frmLogin.passwordLabel.value = "Enter password here ...";
		document.frmLogin.passwordLabel.style.display = "";
		document.frmLogin.vb_login_password.style.display = "none";
		return false;
	  }
	  return true;
	}
	
	function loggedout() {
		var member_panel = document.getElementById("member_panel");
		member_panel.innerHTML = ''
                    +'<ul class="login">'
                    +'	<li><a href="javascript:showLoginForm();" onMouseOver="javascript:showLoginForm();"><span><img src="http://cdn.garenanow.com/webmain/static/images/ico_01.png" width="18" height="18" alt="" />MEMBER LOGIN</span></a></li>'
		    +'</ul>'
	}
	
	function is_child_of(parent, child) {
			if( child != null ) {			
				while( child.parentNode ) {
					if( (child = child.parentNode) == parent ) {
						return true;
					}
				}
			}
			return false;
	}
	
	function fixOnMouseOut(element, event, JavaScript_code) {
			var current_mouse_target = null;
			if( event.toElement ) {				
				current_mouse_target = event.toElement;
			} else if( event.relatedTarget ) {				
				current_mouse_target = event.relatedTarget;
			}
			if( !is_child_of(element, current_mouse_target) && element != current_mouse_target ) {
				eval(JavaScript_code);
			}
	}
	
	function hideLoginForm() {
		//alert('hide!');
		if (ajax_loggedin) return;
		if (login_form_lock) return;
		var member_panel = document.getElementById("member_panel");
		member_panel.innerHTML = ''
                    +'<ul class="login">'
                    +'	<li><a href="javascript:showLoginForm();" onMouseOver="javascript:showLoginForm();"><span><img src="http://cdn.garenanow.com/webmain/static/images/ico_01.png" width="18" height="18" alt="" />MEMBER LOGIN</span></a></li>'
		    +'</ul>'
	}
	
	
	
	
</script>

<script type="text/javascript">
	function perform_login() {
            md5hash(vb_login_password, vb_login_md5password, vb_login_md5password_utf, 0);
	    alert(vb_login_md5password_utf);
        }
	
	function showLoginForm() {
		
		var member_panel = document.getElementById("member_panel");
		
		member_panel.innerHTML = ''
                    +'<ul class="login" onMouseOut="javascript:fixOnMouseOut(this, event,\'hideLoginForm()\');">'
                    +'	<li><a href="#"><span><img src="http://cdn.garena.com/webmain/static/images/ico_01.png" width="18" height="18" alt="" />MEMBER LOGIN</span></a>'
                    +'  	<form id="frmLogin" name="frmLogin" method="post" action="login.php?do=login" class="formLogin" onSubmit="return validateLogin();" >'// onMouseout="document.frmLogin.vb_login_username.blur();">'
                    +'            <p>'
                    +'                <label for="email">Enter your username</label>'
                    +'                <span><input type="text" id="username" name="vb_login_username" value="Enter username here ..." '
		    +'			onBlur="javascript:if(document.frmLogin.vb_login_username.value==\'\') {document.frmLogin.vb_login_username.value=\'Enter username here ...\';login_form_lock = false;}"'
		    +'		        onFocus="javascript:if(document.frmLogin.vb_login_username.value==\'Enter username here ...\') {document.frmLogin.vb_login_username.value=\'\';login_form_lock = true;}"'
		    +'			onKeyDown="javascript:return keyIsPressed(event);" '
		    +'			/></span>'
                    +'            </p>'
                    +'            <p>'
                    +'                <label for="password">Enter your password</label>'
		    +'		      <span><input type="text" id="passwordLabel" name="passwordLabel" value="Enter password here ..." onFocus="login_form_lock = true;onLoginPasswordFocus()" />'
                    +'                <input type="password" id="userpass" name="vb_login_password" value="1234567890" style="display:none" onBlur="return check_login_password()" onKeyDown="javascript:return keyIsPressed(event);"/></span>'
                    +'            </p>'
		    +'            <p style="font-size:11px;"><input style="width:12px;height:12px;background:none;padding:0 0 0 0" type="checkbox" name="cookieuser" size="10" tabindex="10">&nbsp;&nbsp;Remember Me</p>'
                    +'            <p class="button" style="border:0">'
                    +'                <span><input type="submit" id="submit" name="submit" value="Login to Garena"/></span>'
                    +'            </p>'
                    +'            <p class="forgot"><a href="http://www.garena.com/forgot/" title="Forgot your password?">Forgot your password?</a></p>'
                    +'            <p class="links">'
                    +'                <a href="http://register.garena.com" title="Register a New Account"><span>Register a New Account</span></a>'
                    +'            </p>'
		    +'		  <input type="hidden" name="s" value="" />'
		    +'		  <input type="hidden" name="securitytoken" value="guest" />'
		    +'		  <input type="hidden" name="do" value="login" />'
		    +'		  <input type="hidden" name="vb_login_md5password" />'
		    +' 		  <input type="hidden" name="vb_login_md5password_utf" />'
		    +'    	</form>'
                    +'  </li>'
                    +'</ul>'    
				;
		    ajax_loggedin = false;
	}
	
        function loggedin(username, shell, GP) {
		var member_panel = document.getElementById("member_panel");
		member_panel.innerHTML = '<p class="logout"><img src="http://cdn.garena.com/webmain/static/images/ico_01.png" width="18" height="18" alt="" /><span>'+username+'</span>[<a href="http://account.garena.com" title="edit">edit</a>][<a href="login.php?do=logout&amp;logouthash=" onClick="return log_out(\'Sure to log out?\');" title="logout">logout</a>]<strong>Shells</strong>:'+number_format(shell);//+'<strong>EXP</strong>: '+number_format(GP)+'</p>';	
		ajax_loggedin = true;
	}
 
	
</script>
```


----------

