【问题标题】:How to verify if WebElement is selected or not如何验证是否选择了 WebElement
【发布时间】:2017-11-02 12:59:38
【问题描述】:

我想验证是否选择了 WebElement。 该元素在 HTML 中存在(当被选中时)为:

<span id="user-settings-price-preview-checkbox" class="user-settings-selector-checkbox active"></span>

以及什么时候没有被选中:

<span id="user-settings-price-preview-checkbox" class="user-settings-selector-checkbox"></span>

如何使用 selenium 和 TestNG 进行验证?

【问题讨论】:

标签: java selenium selenium-webdriver webdriver getattribute


【解决方案1】:

要验证是否选择了WebElement,您可以尝试:

    String attr = driver.findElement(By.id("user-settings-price-preview-checkbox")).getAttribute("class");
    if(attr.contains("active"))
        System.out.println("WebElement selected");
    else
        System.out.println("WebElement NOT selected");

【讨论】:

  • 这就是我要找的!多谢!
  • 不是这样做的方式,因为状态是通过自定义样式应用的。您需要评估计算的样式以验证元素是否具有预期的颜色、边框...检查类是无用的,因为如果匹配类 active 的 CSS 被重命名、丢失或由于另一个覆盖样式的 CSS 规则而被忽略。
【解决方案2】:

代码 sn-p:

String classAttribute = driver.findElement(By.id("user-settings-price-preview-checkbox")).getAttribute("class");

boolean isItemSelected = classAttribute.endsWith("active");

Assert.assertTrue(isItemSelected);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-12-11
    • 2010-12-06
    • 2015-06-24
    • 2018-03-23
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多