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

时间:2014-08-26 06:40:50

标签: java selenium-webdriver

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

4 个答案:

答案 0 :(得分:5)

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

希望它有所帮助!

答案 1 :(得分:1)

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

答案 2 :(得分:1)

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.

答案 3 :(得分:0)

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