Just I am trying to get entered text from the text field when value is empty.

Below is my HTML:

<input type="text" style="width: 203px" value="" maxlength="32" name="firstName"/>

I tried using getAttribute("value") but it didn't work. Please suggest how to do it using selenium webdriver with java.

Also screenshot attached.

enter image description here

  • You can try with WebElement. WebElement Fname = driver.findElement(By.xpath("//input[@name='firstName']")); Fname.getText(); Let me know if you have any doubts.....!
    – Bharat Mane
    Commented May 14, 2016 at 13:08
  • @BharatMane: No it's not working.
    – Selenium
    Commented May 14, 2016 at 13:21
  • get the placeholder by writing xpath Commented May 18, 2016 at 9:08

11 Answers 11


When are you trying to get a value? before entering or after entering it in the text field?

  WebElement fName=driver.findElement(By.name("firstName"));

Above code should return text entered.


Basically the problem seems like, the attribute "value" is not updating automatically, so after entering the text into textbox click somewhere else on the page and then use the method getAttribute("value")
hope it will works......


Use following code:

WebElement firstName = driver.findElement(By.xpath("//input[@name='firstName']"));
String value = firstName.getText();

I was facing the similar challenge in my angular (v 5.0.0) app. So, I've performed below steps to get the input text:

  1. Right click on any input element.
  2. Get the formControllerName attribute value.
  3. Construct the xpath as given format, XPath = //input[@ng-reflect-name=“<*formcontrolname*>“]

Code snippet:

  .then((value) => {
   console.log('Input text value ---', value);  
// output: Input text value --- <Text available in input box>

Hope it helps.


It would help to check for other javascript attributes if getText is not working.

How about if you try:


If that does not work, double check the element you are getting the text from. Are there any children elements for the input element?


From your picture, I guess that you just enter the text and still not Save, so it is not updated and you cannot get it.

--> Try to save it, then open it again, I think you can get the text easily.

If the text already Save, try to inspect element and search "Administrator" text to see where it is.

WebElement ele = driver.findElement(By.name("lastname"));
ele.sendKeys("Some text");
System.out.println(ele.getText()); ---> this does not work 
System.out.println(ele.getAttribute("value")); --> this works 

To capture dynamic text from a text box, the below code works-

WebElement ele = driver.findElement(By.name("lastname"));

Try this code:

WebElement  element = driver.findElementByName("firstName");
String text = element.getText();

I had the same problem where the text I need to get is pre-entered or is a return value from js script. I was able to get the text by using:

List<WebElement> elements = driver.findElements(By.id("ID"));

In my case, the class contains only one web-element so when I'm using:

String text = elements.get(0).getAttribute("value");

I get the text I need.

Hope it helps.


Whenever you enter anything in text input field, it will store value field in HTML. So if you want to retrieve the value entered in input field later in your program, then just use WebElement.getAttribute("Value");

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.