Selenium WebDriver C#:即使禁用了元素,Element.Enabled也返回True

时间:2020-02-27 10:22:55

标签: c# selenium-webdriver

我需要验证是否禁用了“保存”按钮。 我使用Xpath(// a [contains(@id,'save')])[1]来定位元素。 但是即使禁用了“保存”按钮,element.Enabled仍返回True。

<a data-info="Save" class="btn btn-primary disabled btn-xs save save_990928 lineItemControl" id="save_990928" data-request-url="/Materials/Save">
   <span class="fa fa-floppy-o fa-lg lineItemControl"></span>
</a>

2 个答案:

答案 0 :(得分:2)

IWebElement.Enabled Property

除以下所有内容外,Enabled属性通常将返回true 明确禁用输入元素。

如果元素没有disabled="disabled"属性,element.Enabled将返回true

您可以解析class属性以检查其是否具有disabled

element.GetAttribute("class").Contains("disabled");

答案 1 :(得分:0)

您可以使用getAttribute,在字符串中获取属性值,然后使用简单的if条件进行比较。

String attributeValue =driver.findElement(By.xpath("xpathExpression")).getAttribute(""); if(attributeValue.contains("disabled")){ //code you want to execute }