Ravikanth Buddhiol
Ravikanth Buddhiol

Reputation: 81

How to check if a field is blank and how to read the entered text?

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

Answers (4)

Vignesh Paramasivam
Vignesh Paramasivam

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

Aaron Williams
Aaron Williams

Reputation: 643

wd.find_element_by_id('foo').get_attribute('value')

Upvotes: 0

Andrey Egorov
Andrey Egorov

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

Sitam Jana
Sitam Jana

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

Related Questions