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

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

11 Answers 11

4

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.

answered May 15, 2016 at 15:41
0
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......

Bharat Mane
6,78512 gold badges42 silver badges69 bronze badges
answered May 16, 2016 at 8:25
1

Use following code:

WebElement firstName = driver.findElement(By.xpath("//input[@name='firstName']"));
String value = firstName.getText();
System.out.println(value);
answered May 16, 2016 at 9:49
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.

Bharat Mane
6,78512 gold badges42 silver badges69 bronze badges
answered Sep 23, 2018 at 5:23
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?

answered May 15, 2016 at 20:40
0
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.

answered May 18, 2016 at 6:58
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 
alecxe
11.4k11 gold badges52 silver badges107 bronze badges
answered Feb 20, 2018 at 8:53
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"));
Bharat Mane
6,78512 gold badges42 silver badges69 bronze badges
answered Jun 10, 2018 at 2:37
0

Try this code:

WebElement element = driver.findElementByName("firstName");
String text = element.getText();
System.out.println(text);
Bharat Mane
6,78512 gold badges42 silver badges69 bronze badges
answered Apr 6, 2018 at 6:15
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.

answered Aug 27, 2019 at 4:44
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");

pavelsaman
4,5581 gold badge15 silver badges37 bronze badges
answered Dec 13, 2019 at 4:47

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.