我需要验证是否禁用了“保存”按钮。 我使用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>
答案 0 :(得分:2)
除以下所有内容外,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
}