Anis Haque
Anis Haque

Reputation: 41

validate sendKeys(input) of selenium Webdriver after enter

I am trying to validate an input like following

element.sendKeys(valueToPut);
String readAfterEnter = element.getText();

element.sendKeys(valueToPut) worked properly But readAfterEnter does not give the expected value, it is allways null.

Upvotes: 4

Views: 6267

Answers (2)

Abhishek Singh
Abhishek Singh

Reputation: 9765

This code will work:

WebElement element = driver.findElement(By.name("nameOfElement"));
String text = element.getAttribute("value");

The getAttribute method returns the value of an attribute of an HTML Tag; for example if I have an input like this:

<input name = "text" type ="text" value ="Hello">

then this webdriver code:

WebElement element = driver.findElement(By.name("text"));
String text = element.getAttribute("value");
System.out.println(text);

will print out 'Hello'.

Upvotes: 3

LaurentG
LaurentG

Reputation: 11787

The WebElement.getText() method does not return the content of the user input. For this you have to use WebElement.getAttribute("value") (see this thread).

Upvotes: 5

Related Questions