Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 337
Anis Haque Avatar asked Feb 16 '23 05:02

Anis Haque


2 Answers

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

like image 154
LaurentG Avatar answered Mar 11 '23 02:03

LaurentG


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

like image 32
Abhishek Singh Avatar answered Mar 11 '23 03:03

Abhishek Singh