I'm trying to write a simple auto-login program in Java.
The login page is "login.php" (not mine!), and it has the following form:
<form action="login.php" method="post" name="FormLogin" onsubmit="return validalogin();">
<div class="mainContentTitolo1">Inserisci i dati</div>
<div class="table">
<div class="tr">
<div class="th">
<label for="username">Nome Utente.</label>
</div>
<div class="td">
<input type="text" name="username" id="username" accesskey="m" tabindex="1" size="20" maxlength="16" value=""/>
</div>
</div>
<div class="tr">
<div class="th">
<label for="password">Password</label>
</div>
<div class="td">
<input type="password" name="password" id="password" accesskey="p" tabindex="2" size="20" maxlength="16" value=""/>
</div>
</div>
<input type="hidden" name="refer" value="$refer" />
<input type="hidden" name="invio" value="1" />
<div class="tr">
<div class="tdcolspanallcenter">
<input type="image" src="../../images/conferma.gif" tabindex="3" alt="Conferma login" onclick="return validalogin();" />
</div>
</div>
<div class="tr">
<div class="tdcolspanallcenter">Collegati
</div>
</div>
</div>
</form>
Unfortunately I can't fill "username" and "password" fields.
I tryed to use a link (with CookieHandler and HttpURLConnection classes) like:
https://.../login.php?username=BROWN&password=FOX
But only "username" field results filled and I recive wrong Cookies.
In browser "source code" I can see as result:
[...]
<input type="text" name="username" id="username" accesskey="m" tabindex="1" size="20" maxlength="16" value="BROWN"/>
[...]
<input type="password" name="password" id="password" accesskey="p" tabindex="2" size="20" maxlength="16" value=""/>
[...]
Where is the problem?
Browsers won't let you pass password field(type) values using cookies or from URL, if they would it would be heaven for bots:).
If you insist on auto filling the password field using a parameter from the URL, then change it to <input type="text" name="password" id="password" accesskey="p" tabindex="2" size="20" maxlength="16" value=""/> since anyway every one can see the password in the URL
Related
I am new to thymeleaf and I working on a login form. I have found a simple example where there is login input text and password fields.
I have included the project in my system. When I click the login button without filling username and password section, a please fill in details ballon pops up. I have not configured it anywhere, but would like to know from it is getting popped up. Below is the code
<div class="row" style="margin-top:20px">
<div class="form-group " align="right">
</br> </br> </br> </br> </br></br> </br> </br> </br>
<form th:action="#{/login}" method="post">
<fieldset>
<h2 class="form-heading">Log in</h2>
<div th:if="${param.error}">
<div class="alert alert-danger">
Invalid username and password.
</div>
</div>
<div th:if="${param.logout}">
<div class="alert alert-info">
You have been logged out.
</div>
</div>
<div class="form-group">
<input type="text" name="username" id="userName" class="form-control input-lg"
placeholder="UserName" required="true" autofocus="true"/>
</div>
<div class="form-group">
<input type="password" name="password" id="password" class="form-control input-lg"
placeholder="Password" required="true"/>
</div>
<div class="row">
<button class="btn btn-default" role="button">Log In</button>
</div>
</div>
</fieldset>
</form>
</div>
</div>
This is really weird but I suddenly cannot login using selenium. I was able to login with my code just a week ago and then I can't anymore.
Here's the html code from the website:
<div class="form-group">
<label for="loginUsername" style="width: 100%; font-weight: normal" aria-label="Username">
<input id="loginUsername" class="form-control" name="username" type="text" placeholder="Username" autocapitalize="off">
</label>
</div>
<div class="form-group">
<label for="loginPassword" style="width: 100%; font-weight: normal" aria-label="Password">
<input id="loginPassword" class="form-control" name="password" type="password" placeholder="Password" autocapitalize="off">
</label>
</div>
And here is my code before the incident:
driver.findElement(By.xpath("//*[#id=\"loginUsername\"]")).sendKeys("username");
driver.findElement(By.xpath("//*[#id=\"loginPassword\"]")).sendKeys("password");
It worked fine before. I even tried changing it up and it looked something like this:
WebElement username = driver.findElement(By.xpath("//*[#id=\"loginUsername\"]")); username.click(); username.clear(); ;username.sendKeys("username");
WebElement password = driver.findElement(By.xpath("//*[#id=\"loginPassword\"]")); password.click(); password.clear(); ;password.sendKeys("password");
I honestly don't know what is wrong with my code and I didn't get any error too. I also tried using "By.name()" or "By.id()" but it still didn't work so I'm really confused. Please help me, thank you!
edited
Here's the full version of the code
<div class="col-sm-4 col-sm-offset-4">
<form action="/login" method="post">
<div class="form-group title">
<img src="/img/RA_LOGO_BLUE.png" alt="Risk Assessment System Logo">
</div>
<div class="form-group">
<label for="loginUsername" style="width: 100%; font-weight: normal" aria-label="Username">
<input id="loginUsername" class="form-control" name="username" type="text" placeholder="Username" autocapitalize="off">
</label>
</div>
<div class="form-group">
<label for="loginPassword" style="width: 100%; font-weight: normal" aria-label="Password">
<input id="loginPassword" class="form-control" name="password" type="password" placeholder="Password" autocapitalize="off">
</label>
</div>
<input id="loginBtn" class="btn" type="submit" value="Login">
</form>
<br>
<div class="text-left">
<span class="fakeLink" data-toggle="modal" data-target="#forgotPasswordModal">Forgot Password?</span><br>
<span class="fakeLink" data-toggle="modal" data-target="#forgotUsernameModal">Forgot Username?</span>
</div>
</div>
To send text to the username and password field you can use the following code block :
driver.findElement(By.xpath("//input[#id='loginUsername']")).sendKeys("username");
driver.findElement(By.xpath("//input[#id='loginPassword']")).sendKeys("password");
I am facing difficulty finding elements using Xpath in selenium. I have a payment page and a field to enter the credit card number. When I simply write a script to enter the credit card number using id it is working. The problem comes when I log into a wallet before entering my CC details. After logging into the wallet, there are two id's by the same name. The script works when I use absolute xpath but that changes when I give the script to someone else.
Html before before into wallet
<form autocomplete="off" name="creditcard-form" method="post" action="submitTransaction?MID=oIVSOk48659529909784&ORDER_ID=PARCEL675442&route=" id="card" class="cc-form validated">
<input type="hidden" name="txnMode" value="CC" />
<input type="hidden" name="txn_Mode" value="CC" />
<input type="hidden" name="channelId" value="WEB" />
<input type="hidden" name="AUTH_MODE" value="3D" />
<input type="hidden" name="CARD_TYPE" id="cardType" value="" />
<input type="hidden" name="walletAmount" id="walletAmountCC" value="0" />
<ul class="grid">
<li class="mb20 card-wrapper">
<label class="mb10" for="cardNumber">ENTER CREDIT CARD NUMBER</label>
<p class="cd">
<input autocomplete="off" type="text" name="" class="ccCardNumber text-input large-input c cardInput type-tel" id="cn" size="16" maxlength="19" style="width: 278px" data-type="cc" value="">
<input type="hidden" name="cardNumber" value="" class="required">
</p>
After logging into Wallet:
<input type="hidden" name="txnMode" value="CC" />
<input type="hidden" name="txn_Mode" value="CC" />
<input type="hidden" name="channelId" value="WEB" />
<input type="hidden" name="AUTH_MODE" value="3D" />
<input type="hidden" name="CARD_TYPE" id="cardType" value="" />
<input type="hidden" name="walletAmount" id="walletAmountCC" value="0" />
<input type="hidden" name="addMoney" value="1" />
<ul class="grid">
<li class="mb20 card-wrapper">
<label class="mb10" for="cardNumber">ENTER CREDIT CARD NUMBER</label>
<p class="cd">
<input autocomplete="off" type="text" name="" class="ccCardNumber text-input large-input c cardInput type-tel" id="cn" size="16" maxlength="19" style="width: 278px" data-type="cc" value="">
<input type="hidden" name="cardNumber" value="" class="required">
</p>
Please help as I am new to programming and can't figure out how to handle this
I have a jsp file with below code
<form:form modelAttribute="user" method="POST" name="loginForm" action="loginProcess" id="loginForm" autocomplete="off">
<input type='hidden' name='remember-me' value="true" />
<div id="login-container">
<spring:bind path="userName">
<c:if test="${status.error}">
<div id="formError" <span><form:errors path="userName" /></span></div>
</c:if>
</spring:bind>
<fieldset>
<ul id="login-form">
<li>
<label for="username"><spring:message code="login.UserName" /></label>
<input id="username" name="username" type="text" <spring:bind path="userName"> <c:if test="${not empty status.error}"> value='<%= (session.getAttribute(UsernamePasswordAuthenticationFilter.SPRING_SECURITY_FORM_PASSWORD_KEY) == null ? userName :session.getAttribute(UsernamePasswordAuthenticationFilter.SPRING_SECURITY_FORM_PASSWORD_KEY)) %>'</c:if> </spring:bind> />
</li>
<li>
<label for="password"><spring:message code="login.Password" /></label>
<input id="password" name="password" type="password" />
</li>
<li>
<div class="buContainer">
<a class="buOrange large" id="submit" href="#" onclick="validateForm()"><spring:message code="login.logIn" /></a>
</div>
</li>
</ul>
</fieldset>
<div class="buContainer">
<a id="forgot-link" href="forgotPassword"><spring:message code="login.forgotPassword" /></a>
</div>
</div>
</form:form>
Here when login fails i need to retain username.I tried
<form:form>
<form:input path="username" />
</form:form>
But i dont know where to use above code, Please help me on this.
We have this tag in our login.jsp, it seems to work OK:
<input id="j_username"
type="text"
name="j_username"
value="${sessionScope.SPRING_SECURITY_LAST_USERNAME}"
placeholder="Username"
class="form-control no-border">
The $sessionScope.SPRING_SECURITY_LAST_USERNAME seems to do the trick.
The following is the HTML code:
<div id="TITLE" class="text">
<label for="widget_polarisCommunityInput_113_title">Title</label>
<input type="text" name="field(TITLE)" id="widget_polarisCommunityInput_113_title">
<div class="error">This field must not be empty</div>
</div>
I tried to get the text "This field must not be empty" as follows by using WebDriver with Java:
driver.findElement(By.xpath("//div[#id='TITLE']/div")).getText();
driver.findElement(By.xpath("//div[#class='error']")).getText();
But, no text was retrieved. How can I do it? What's the wrong with my code?
Yes, there is a form. HTMl code including form as belows:
<form action="http://community.sandbox.no/content/save.do;jsessionid=F2BF733599D8D9F812B89ACBC20D37C5" method="post">
<div id="widget_polarisCommunityInput_113_leftcolumn">
<input type="hidden" value="article" name="articleType">
<input type="hidden" value="published" name="state">
<input type="hidden" value=" " name="successUrl">
<input type="hidden" value="" name="articleId">
<input type="hidden" value="http://sandbox.no/community/article/" name="redirect_url">
<input type="hidden" value="113" name="widget_id">
<div id="TITLE" class="text">
<label for="widget_polarisCommunityInput_113_title">Title</label>
<input type="text" name="field(TITLE)" id="widget_polarisCommunityInput_113_title">
<div class="error">This field must not be empty</div>
</div>
</form>
You can try as:
driver.findElement(By.xpath("//form/div[#id='widget_polarisCommunityInput_113_leftcolumn']/div[#id='TITLE']/div")).getText();