检查按钮是否启用而没有启用或禁用属性

问题描述

我想检查按钮是否启用。遗憾的是,没有这样的属性可以区分按钮是启用还是禁用。

HTML 代码如下:

<input _ngcontent-iuv-c99="" type="checkBox" id="IDofButton" name="NameOfButton" formcontrolname="XYZ" class="ng-untouched ng-pristine ng-valid">
    <i _ngcontent-iuv-c99="" tabindex="0"></i>
[开关状态下的按钮图像][https://i.stack.imgur.com/keZWq.png]

当我们打开或关闭按钮时,没有看到任何变化。 如何让 WebDriver 知道按钮是否启用。

解决方法

Selenium 使用 html disabled 属性知道按钮是否启用。你可以使用 is enabled 方法

https://www.selenium.dev/selenium/docs/api/java/org/openqa/selenium/WebElement.html#isEnabled()

element.isEnabled()

这将返回真或假。这仅在使用 html disable 属性实现启用和禁用时才有效。

https://www.w3schools.com/tags/att_disabled.asp

如果使用 css 样式启用或禁用按钮,那么您应该检查特定的类更改或任何属性更改以验证按钮是否被禁用。

在这种情况下使用

element.getAttribute("class") 

验证它是否符合预期。

但在这种情况下,即使 class css 样式有问题,它也不会被 selenium 捕获,这就是为什么探索性手动测试对于 UI 仍然很重要