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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/25499551/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 00:31:03  来源:igfitidea点击:

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

javaselenium-webdriver

提问by Ravikanth Buddhiol

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?

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

回答by Vignesh Paramasivam

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");
}

回答by Sitam Jana

Access valueattribute of the <input>web element. Following is an example:

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");
}

Hope it helps!

希望能帮助到你!

回答by Andrey Egorov

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.

回答by Aaron Williams

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

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