【问题标题】:How to find attribute without value is present or not in HTML tag using Selenium?如何使用 Selenium 在 HTML 标记中查找没有值的属性?
【发布时间】:2017-05-26 09:32:33
【问题描述】:

例如:收音机 - 选中

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

例如:收音机 - 未选中

<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; 
}

【问题讨论】:

  • 您可以考虑与我们分享您的作品吗?谢谢
  • 请不要(错误)使用 cmets 部分发布代码,而不是 edit 您的问题将其发布在那里。

标签: java html selenium attributes


【解决方案1】:

只要检查属性值是否不为空。

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

【讨论】:

    【解决方案2】:

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

    【讨论】:

      【解决方案3】:

      使用 Jquery 属性

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

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

      【讨论】:

      • 问题是关于硒的。为此目的使用 jQuery 是无法实现的。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-06-12
      • 1970-01-01
      • 1970-01-01
      • 2021-10-20
      • 1970-01-01
      • 2017-03-29
      相关资源
      最近更新 更多