【问题标题】:How to verify an attribute is present in an element using Selenium WebDriver?如何使用 Selenium WebDriver 验证元素中是否存在属性?
【发布时间】:2014-01-05 20:32:54
【问题描述】:

我的屏幕上有很多单选按钮。当单选按钮被选中时,它的属性为选中。当单选按钮未被选中时,checked 属性不存在。我想创建一个在元素不存在时会通过的方法。

我正在使用 selenium webdriver 和 java。我知道我可以使用getSingleElement(XXX).getAttribute(XXX) 检索属性。我只是不确定如何验证一个属性是否不存在,以及如何在它不存在时通过测试(如果它存在则失败)。

当单选按钮被选中时

<input id="ctl00_cphMainContent_ctl00_iq1_response_0" type="radio" name="ctl00$cphMainContent$ctl00$iq1$response" value="1" checked="checked"> 

当单选按钮未被选中时

<input id="ctl00_cphMainContent_ctl00_iq1_response_0" type="radio" name="ctl00$cphMainContent$ctl00$iq1$response" value="1">

我希望在 check 属性不存在时通过测试

【问题讨论】:

  • 选中单选按钮时 当未选中单选按钮时 我希望在未选中属性时通过测试呈现

标签: java selenium selenium-webdriver webdriver


【解决方案1】:

here:

getAttribute(java.lang.String name)

返回: 属性的当前值,如果该值未设置,则为 null。

使用您正在使用的任何测试框架来断言该值为空

Assert.IsNull(getSingleElement(XXX).getAttribute("checked"));

【讨论】:

  • 奇怪...我似乎得到了 org.openqa.selenium.NoSuchElementException。
【解决方案2】:

您可以创建一个方法来正确处理它。注意以下是 C#/Java 混合风格,需要稍作调整才能编译。

private boolean isAttribtuePresent(WebElement element, String attribute) {
    Boolean result = false;
    try {
        String value = element.getAttribute(attribute);
        if (value != null){
            result = true;
        }
    } catch (Exception e) {}

    return result;
}

使用方法:

WebElement input = driver.findElement(By.cssSelector("input[name*='response']"));
Boolean checked = isAttribtuePresent(input, "checked");
// do your assertion here

【讨论】:

  • 为什么使用 try catch。元素上不存在属性时会引发异常吗?
  • @PraveenRawat:这是 5 年前创建的 C#/Java 混合伪代码,旨在提供一个想法。将其更改为适合您的任何内容。
【解决方案3】:

用于断言单选按钮被选中

Assert.assertTrue(element.isSelected());

用于断言单选按钮未被选中

Assert.assertFalse(element.isSelected());

用于断言元素中存在属性

Assert.assertEquals(element.getAttribute(attributeName), expectedAttributeValue);

【讨论】:

    【解决方案4】:

    复选框有时会很棘手,因为属性 checked 后面可能没有属性值。

    如果您只关心属性的存在,简单的检查如下所示:

     boolean hasAttr_css = driver.findElementsByCssSelector("#input_id[checked]").isEmpty();
    

    【讨论】:

      【解决方案5】:
      try { 
      if (webdriver.findElement(By.identificationMethod(value)).getAttribute(value) != null) {
             passed = false;
          }
      } catch (Exception e) {
          passed = true;
      }
      

      当我需要检查元素是否存在时,这是我使用的那种解决方案,但是必须注意,NullPointer 错误没有被 NoSuchElement 异常捕获。

      【讨论】:

        【解决方案6】:

        很遗憾,接受的答案在报告的案例中无效。 出于某种原因,Cr 和 FF 不存在的属性返回空字符串而不是 null。 问题链接:https://github.com/SeleniumHQ/selenium/issues/2525

        【讨论】:

        • 嗨。我遇到了这个问题。当属性不存在时,有没有办法捕捉?我的代码现在看起来像这样if(altAttribute == null || altAttribute =="" || altAttribute == " ") 而且它没有捕捉到we.getAttribute("alt"); 返回的任何东西......
        【解决方案7】:

        由于没有一个答案对我有帮助,我想分享我最后采用的非常简单的解决方案:

        static ExpectedCondition<Boolean> isAttributePresent(WebElement element, String attrName) {
            JavascriptExecutor executor = (JavascriptExecutor) getDriver();
            String script = "return arguments[0].getAttributeNames();";
            var attributes = executor.executeScript(script, element);
            return input -> ((ArrayList) attributes).contains(attrName);
        }
        

        我希望它可以帮助某人。我将它用于 wait.until(condition) 方案

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2015-05-29
          • 2015-08-14
          • 1970-01-01
          • 2011-12-20
          • 1970-01-01
          • 2013-10-30
          • 2012-02-24
          • 1970-01-01
          相关资源
          最近更新 更多