c# - 使用 Selenium WebDriver 验证文本字段不为空

标签 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 是:

<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/

相关文章:

c# - 在另一台计算机上运行时找不到声音文件位置

selenium - 基于父子节点的 CSS 选择器?

java - TestNG 和 Selenium : Perform CRUD operations on entities with depedencies

python - 如何使用 selenium python 绑定(bind)的相对 xpath 来定位 dom 元素?

java - 如何添加webElement? e.getText() 以字符串格式给出输出,我想添加此输出数字

c# - 使用 C# 在 Selenium WebDriver 中断言、验证和其他命令

selenium-webdriver - 无法在 selenium 中打开新选项卡或新窗口

c# - 如何将临时类转换为json?

c# - 如何加载列表框中的所有项目而不仅仅是视觉项目

c# - 从运行时分析生成测试