如何检查字段是否为空以及如何读取输入的文本?

Rav*_*iol 3 java selenium-webdriver

如何检查文本字段是否为空白,如果给出如何将该文本存储到变量中,则不给出输入?

Sit*_*ana 5

Web元素的访问属性<input>.以下是一个例子:

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");
}
Run Code Online (Sandbox Code Playgroud)

希望能帮助到你!


Vig*_*vam 2

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");
}
Run Code Online (Sandbox Code Playgroud)