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
.
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).
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'.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With