getText() in WebDriver using Java - java

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();

Related

How to pass model's object to thymeleaf, and to contorller as hidden in thymeleaf?

How to pass model's object to thymeleaf, and to contorller as hidden in thymeleaf?
model.addAttribute("member", member);
model.addAttribute("item", item);
model.addAttribute("poi", poi);
model.addAttribute("myModel", myModel);
model.addAttribute("allPlans", allPlans);
model.addAttribute("allPois", allPois);
model.addAttribute("itemParam",itemParam);
This is controller1.
controller1 passes 7 objects to item.
<input type="hidden" th:field=${member}>
<input type="hidden" th:field=${item}>
<input type="hidden" th:field=${poi}>
<input type="hidden" th:field=${myModel}>
<input type="hidden" th:field=${allPlans}>
<input type="hidden" th:field=${allPois}>
<input type="hidden" th:field=${itemParam}>
This is the thymeleaf code.
Here is the result:
<input type="hidden" id="" name="" value="">
<input type="hidden" id="" name="" value="">
<input type="hidden" id="" name="" value="">
<input type="hidden" id="" name="" value="">
<input type="hidden" id="" name="" value="">
<input type="hidden" id="" name="" value="">
<input type="hidden" id="" name="" value="">
How can I pass 7 objects intact to the controller?
I think there are two ways to do this:
Method 1
Instead of using th:field use html id & name. Set value using th:value
<input type="hidden" id="yourId1" name="yourName1" th:value="${item}">
Method 2: Use preprocessing
<input type="hidden" th:field="__${item}__">

Thymeleaf if else [duplicate]

This question already has answers here:
How to do if-else in Thymeleaf?
(14 answers)
Closed 1 year ago.
I don't know how to wrote this kind of insruction using Thyeleaf.
I've got my view made with html.
<#if updateClient??>
<h2>Modifica del prodotto - ${updateClient.nome}</h2>
<div style="margin: 20px;">
<form method="POST" action="update" id="updateClient">
<input type="hidden" name="id" value="${updateClient.id}"/>
<div>
<label for="name">Name</label>
<input type="text" name="nome" id="nome" value="${updateClient.nome}" />
</div>
<div>
<input type="submit" name="invia" value="Update" />
</div>
</form>
</div>
<#else>
<h2>New Client</h2>
<div style="margin: 20px;">
<form method="POST" action="add" id="newDataClient">
<div>
<label for="nome">Nome</label>
<input type="text" name="nome" id="nome" value="" />
</div>
<div>
<input type="submit" name="invia" value="Add" />
</div>
</form>
</div>
</#if>
This was written using FreeMarker, but I need to use Thymeleaf.
I know simple Thymeleaf instructions, but I don't know how to do this. I read Thymeleaf instructions, and I find how to iterate a list of variables, but not how to create this structure using if and else. And if it's possible create something like this.
<div th:if="${updateClient.nome}">
<div>Edit Form</div>
</div>
<!-- ELSE -->
<div th:unless="${updateClient.nome}">
<div>New Form</div>
</div>

Change button text on submit with velocity engine / java

So now I got a admin panel for my website I decided to try some small things but I am not getting there yet.
So whenever I disable or enable a user I get a nice good/bad notification saying what happened but ofcourse the button will stay the same. Is their a way to change this according to the function?
#parse("/velocity/layout.vm")
##mainLayout()
<table id="adminPanel">
<tr>
<th>Email</th>
<th>Name</th>
<th>Reset Password</th>
<th>IsActive</th>
<th>Delete</th>
</tr>
#if($resetpassword)
<p class="good notification">$msg.get("RESET_PASSWORD")</p>
#elseif($blockuser)
<p class="bad notification">$msg.get("BLOCK_USER")</p>
#elseif($unblockuser)
<p class="good notification">$msg.get("UNBLOCK_USER")</p>
#elseif($deleteuser)
<p class="bad notification">$msg.get("DELETE_USER")</p>
#end
#foreach($user in $users)
#if($user.getIsadmin())
<div class="col">
<tr>
<th>$user.getEmail()</th>
<th>$user.getFirstname() $user.getSurname()</th>
<th><form method="post">
<input type="hidden" name="post" value="reset">
<input type="hidden" name="email" value="$user.getEmail()">
<input type="submit" value="Reset Password" disabled>
</form></th></th>
<th><form method="post">
<input type="hidden" name="post" value="block">
<input type="hidden" name="email" value="$$user.getEmail()">
<input type="submit" value="IsActive" disabled>
</form></th></th>
<th><form method="post">
<input type="hidden" name="post" value="delete">
<input type="hidden" name="email" value="$user.getEmail()">
<input id="button" type="submit" value="Delete" disabled>
</form></th>
</tr>
</div>
#else
<div class="col">
<tr>
<th>$user.getEmail()</th>
<th>$user.getFirstname() $user.getSurname()</th>
<th><form method="post">
<input type="hidden" name="post" value="reset">
<input type="hidden" name="email" value="$user.getEmail()">
<input type="submit" value="Reset Password">
</form></th></th>
<th><form method="post">
<input type="hidden" name="post" value="block">
<input type="hidden" name="email" value="$user.getEmail()">
<input type="submit" value="Disable">
</form></th></th>
<th><form method="post">
<input type="hidden" name="post" value="delete">
<input type="hidden" name="email" value="$user.getEmail()">
<input type="submit" value="Delete">
</form></th>
</tr>
</div>
#end
#end
</table>
#end
It is very unclear in your question about what buttons you want the text to be changed. Since I see only two buttons in your code, I can recommend you to do something like this (assuming that the User object has enough details to know whether a user is blocked or not).
<input type="button" #if ($user.isBlocked()) value="Unblock" #else value="Block" #end>

Elements change after logging into account in Selenium

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

How to fill form's fields programmatically?

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

Categories