Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 669
Ravikanth Buddhiol Avatar asked Aug 26 '14 06:08

Ravikanth Buddhiol


People also ask

How do you read the text from the hidden elements?

WebElement hiddenDiv = seleniumDriver. findElement(By.id("hidden_div")); String n = hiddenDiv. getText(); // does not work (returns "" as expected) String script = "return arguments[0]. innerText"; n = (String) ((JavascriptExecutor) driver).

How do you check if an element is empty in selenium?

New Selenium IDE This can be determined with the help of findElements() method. It returns the list of elements matching the locator we passed as an argument to that method. In case there is no matching element, an empty list [having size = 0] will be returned.

What is the method to read the value in the text field in selenium?

We can get the entered text from a textbox in Selenium webdriver. To obtain the value attribute of an element in the html document, we have to use the getAttribute() method. Then the value is passed as a parameter to the method.


1 Answers

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!

like image 124
Sitam Jana Avatar answered Oct 25 '22 13:10

Sitam Jana