假设我想在继续点击提交按钮之前验证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 是:
<input name="text_input" value="Fred" id="text_input" tabindex="6" class="label" type="text">
最佳答案
如果您有这样的输入:
<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
}
关于c# - 使用 Selenium WebDriver 验证文本字段不为空,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22597184/