【问题标题】:How to check if a button is disabled using css in selenium webdriver [duplicate]如何在 selenium webdriver 中使用 css 检查按钮是否被禁用 [重复]
【发布时间】:2019-03-01 19:30:57
【问题描述】:

我需要一些帮助来检查按钮是否被禁用,附上 dom 的屏幕截图以供参考,尝试了 WebDriver 中的 isEnabled() 函数,但它返回 true。

【问题讨论】:

    标签: java selenium selenium-webdriver xpath css-selectors


    【解决方案1】:

    您可以检查元素是否具有disabled 属性。如果存在,您将获得String 结果,如果不存在,您将获得null

    WebElement button = driver.findElement(locator);
    bool isDisabled = button.getAttribute("disabled") != null;
    

    【讨论】:

      【解决方案2】:

      有两种方法可以检查按钮是否禁用如下:

      • 使用try-catch{}

        try {
            //css
            driver.findElement(By.cssSelector("fieldset.checkbox button.calvary-button[disabled]"));
            //xpath
            //driver.findElement(By.xpath("//button[@class='calvary-button' and contains(.,'Continue')][@disabled]"));
            System.out.println("Button is disabled");
        } catch (NoSuchElementException e) {
            System.out.println("Button is enabled");
        }
        
      • 使用findElements() 和断言零长度响应:

        if(driver.findElements(By.cssSelector("fieldset.checkbox button.calvary-button[disabled]")).size()>0)
            System.out.println("Button is disabled");
        else
            System.out.println("Button is enabled");
        

      【讨论】:

      • 这会检查按钮是否存在,而不是是否禁用。
      • 没错,如果按钮与 attribute disabled 一起存在,例如[disabled]。系统输出是可配置的。
      猜你喜欢
      • 2017-12-08
      • 2011-09-20
      • 2014-06-15
      • 2014-11-17
      • 1970-01-01
      • 2022-01-09
      • 2019-10-28
      • 2011-04-25
      • 2014-07-11
      相关资源
      最近更新 更多