Reputation: 81
How to check whether a text field is blank i.e. no input is given if given how to store that text into a variable?
Upvotes: 7
Views: 77718
Reputation: 2470
WebElement ele = driver.findElement(by locator)); //find the text field
if (ele.getAttribute("value").isEmpty()) {
//Do something if the text field is empty
}
else {
//Store the value
String store=ele.getAttribute("value");
}
Upvotes: 2
Reputation: 1269
var element = driver.findElement(webdriver.By.id("your elements id"));
//store text
var text = element.getText();
//store value
var value = element.getAttribute("value");
//after that you can do anything you want with these variables.
Upvotes: 1
Reputation: 3129
Access value attribute of the <input>
web element. Following is an example:
WebElement inputBox = driver.findElement(By.id("inputBoxId"));
String textInsideInputBox = inputBox.getAttribute("value");
// Check whether input field is blank
if(textInsideInputBox.isEmpty())
{
System.out.println("Input field is empty");
}
Hope it helps!
Upvotes: 10