使用Selenium WebDriver验证文本字段是否为空

时间:2014-03-23 21:16:08

标签: c# selenium selenium-webdriver webdriver

假设我要验证ID为" text_input"的文本输入字段。在继续单击提交按钮之前不为空(包含文本),例如:

// verify that html element id "text_input" is not null

var button_Save = Driver.Instance.FindElement(By.Id("submit"));
button_Save.Click();

怎么可能这样呢?

我要验证的元素的HTML不为null:

<input name="text_input" value="Fred" id="text_input" tabindex="6" class="label" type="text">

1 个答案:

答案 0 :(得分:4)

如果你有这样的输入:

<input name="text_input" value="Fred" id="text_input" tabindex="6" class="label" type="text">

然后您可以使用element.GetAttribute("value")来获取其价值。来源here

var text_input = Driver.Instance.FindElement(By.Id("text_input"));
if (!String.IsNullOrEmpty(text_input.GetAttribute("value"))) {
    var button_Save = Driver.Instance.FindElement(By.Id("submit"));
    button_Save.Click();
} else {
    // if you want to do something
}