如何使用Selenium在HTML标记中是否存在没有值的属性?

时间:2017-05-26 09:32:34

标签: java html selenium attributes

Ex:收音机 - 检查

<input id="rdo" type="radio" name="nrdo" value="1" checked>

Ex:Radio - 未选中

<input id="rdo" type="radio" name="nrdo" value="1"> 

需要在HTML标记上找到没有值的属性,或者不使用Selenium。

此代码仅在属性具有值时才有效:

public boolean isAttribtuePresent(String element, String attribute) { 
    Boolean result = false; 
    String value = "vvv"; 
    // element = "Month ''May'' @xpath=//button[text()='Jun']"; 
    try { 
        value = webElement.getAttribute(attribute); 
        if (value != null) { 
            result = true; 
        } 
    } catch (Exception e) {  
        System.out.print(e.toString()); 
    } 
    System.out.print("***"+value+"*****"); 
    return result; 
}

3 个答案:

答案 0 :(得分:0)

检查attributre值是否为空。

function hasAttr(el, attr) {
  return el.getAttribute(attr) !== null
}

console.log(hasAttr(rdo, 'checked'))
console.log(hasAttr(rdo2, 'checked'))
<input id="rdo" type="radio" name="nrdo" value="1" checked>
<input id="rdo2" type="radio" name="nrdo" value="1">

答案 1 :(得分:0)

使用Jquery attr

http://api.jquery.com/attr/

$.each($( ":input" ),function(i,o){
 alert($(this).attr("value")==undefined);
})

答案 2 :(得分:0)

element.getAttribute(attributeName)都返回null 1.没有价值的属性 2.属性不存在