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