Prasanta Biswas Prasanta Biswas - 5 months ago 35
Java Question

How to get entered text from a textbox having no value attribute in selenium

My element looks like this:

<input name="ctl00$PlaceHolderPopUp$ReviewName"
type="text" maxlength="255" id="PlaceHolderPopUp_ReviewName"
class="giInput_two inputHeight_two"
style="height:20px;width:100%;">


Retrieving code:

String value=driver.findElement(By.xpath("//*@id='PlaceHolderPopUp_ReviewName']")).getText().trim();


I want to enter some text in it and retrieve the text before saving. I used
getText()
method but it is not retrieving any text.

Answer

If target element is input element, you should try as below :-

WebElement el=driver.findElement(By.xpath("//*@id='PlaceHolderPopUp_ReviewName']"));
String value = el.getAttribute("value");

Note :- input element always contains entered text in their attribute property value. It does matter that their attribute value present or not. Hope it will help you...:)