5

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 May 14 '16 at 13:08
  • @BharatMane: No it's not working. – Selenium May 14 '16 at 13:21
  • get the placeholder by writing xpath – sameer joshi May 18 '16 at 9:08

11 Answers 11

3

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"));
  fName.sendKeys("Admin");
  System.out.println(fName.getText());

Above code should return text entered.

1

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......

1

Use following code:

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

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:

driver.findElement(by.xpath('//input[@ng-reflect-name="<formControllerName>"]')).getAttribute('value')
  .then((value) => {
   console.log('Input text value ---', value);  
// output: Input text value --- <Text available in input box>
  };
});

Hope it helps.

0

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

How about if you try:

el.getAttribute("innerHTML");

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

0

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.

0
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 
0

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

WebElement ele = driver.findElement(By.name("lastname"));
System.out.println(ele.getAttribute("value"));
0

Try this code:

WebElement  element = driver.findElementByName("firstName");
String text = element.getText();
System.out.println(text);
0

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.

0

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

New contributor
Sunilkumar A H is a new contributor to this site. Take care in asking for clarification, commenting, and answering. Check out our Code of Conduct.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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