java 即使默认情况下禁用按钮,Button.isEnabled() 也会返回 true
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/44563408/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Button.isEnabled() returns true even though the button is disabled by default
提问by SKV
While testing I got a road block where I have a button in a WebPage which is disabled by default. I am using Selenium WebDriver to test if the button is disabled by default the boolean is always returning true.
在测试时,我遇到了一个障碍,我在网页中有一个按钮,默认情况下是禁用的。我正在使用 Selenium WebDriver 来测试默认情况下按钮是否被禁用,布尔值总是返回 true。
Boolean buttonStatus = (button XPath).isEnabled
It will be great if someone can help me
如果有人可以帮助我,那就太好了
HTML Information:
HTML信息:
<div class="commandbutton commandbutton--theme-disabled commandbutton--recommended">
<button class="commandbutton-button commandbutton-button--disabled" type="button" tabindex="-1">
回答by Guy
From isEnabled docs
This will generally return true for everything but disabled input elements.
这通常会为除禁用输入元素之外的所有内容返回 true。
But it will work on buttons as well. However, isEnabled()
checks for the disabled
attribute. If the button is disabled by JavaScript or any other means isEnabled()
won't detect it.
但它也适用于按钮。但是,isEnabled()
检查disabled
属性。如果按钮被 JavaScript 或任何其他方式禁用,isEnabled()
则不会检测到它。
My guess is the button has other classes when it is enabled or disabled. For example, when enabled it probably won't have commandbutton-button--disabled
class. You can check for it
我的猜测是该按钮在启用或禁用时具有其他类。例如,当启用它可能不会有commandbutton-button--disabled
类。你可以检查一下
WebElement button = driver.findElement(By.xpath("button XPath"));
String classes = button.getAttribute("class");
boolean isDisabled = classes.contains("commandbutton-button--disabled");
回答by Alex Bruce
isEnabled can only tell you the button works fine, you need to check the class attribute to check is the button is enabled.
isEnabled 只能告诉你按钮工作正常,你需要检查class属性来检查按钮是否被启用。
回答by tomasomsk
I had the same problem. But my elements on the page were very strange. Some of them selenium could click although they were not clickable, some of them selenium couldn't click, but could send keys to them. After a few hours of thinking, I have wrote universal method, that checks if elements is enabled or not.
我有同样的问题。但是我在页面上的元素很奇怪。有些selenium虽然不能点击但可以点击,有些selenium不能点击,但可以向他们发送密钥。经过几个小时的思考,我编写了通用方法,用于检查元素是否已启用。
After talking with programmer, I have known, that they use on this page some special Select, and it looks like Div with Input in it. And he says, that I can check it disabling by checking attribute Class of Div. If there is 'select2-container-disabled' then this Input is disabled.
与程序员交谈后,我知道,他们在此页面上使用了一些特殊的 Select,它看起来像带有 Input 的 Div。他说,我可以通过检查属性 Class of Div 来检查它是否禁用。如果有“select2-container-disabled”,则该输入被禁用。
And I change my method. Now it looks like that:
我改变了我的方法。现在看起来像这样:
public boolean isNotClickable(WebElement... elements) {
List<WebElement> elementsChecked = new ArrayList<>();
List<WebElement> elementsToCheckByClass = new ArrayList<>();
List<WebElement> elementsToCheckByClick = new ArrayList<>();
List<WebElement> elementsToCheckBySendKeys = new ArrayList<>();
for (WebElement checkedElement : elements) {
log.info("Checking, that element [" + getLocator(checkedElement) + "] is not clickable by isEnabled()");
if (checkedElement.isEnabled()) {
elementsToCheckByClass.add(checkedElement);
} else {
elementsChecked.add(checkedElement);
}
}
if (!elementsToCheckByClass.isEmpty()) {
for (WebElement checkedByClassElement : elementsToCheckByClass) {
log.info("Checking, that element [" + getLocator(checkedByClassElement) + "] is not clickable by class");
String classOfElement = checkedByClassElement.getAttribute("class");
List<String> classes = new ArrayList<>(Arrays.asList(classOfElement.split(" ")));
if (!classes.contains("select2-container-disabled")) {
elementsToCheckByClick.add(checkedByClassElement);
} else {
elementsChecked.add(checkedByClassElement);
}
}
}
if (!elementsToCheckByClick.isEmpty()) {
WebDriverWait wait = new WebDriverWait(driverUtils.getDriver(), 1);
for (WebElement checkedByClickElement : elementsToCheckByClick) {
log.info("Checking, that element [" + getLocator(checkedByClickElement) + "] is not clickable by clicking it");
try {
wait.until(elementToBeClickable(checkedByClickElement));
elementsToCheckBySendKeys.add(checkedByClickElement);
} catch (Exception e) {
elementsChecked.add(checkedByClickElement);
}
}
}
if (!elementsToCheckBySendKeys.isEmpty()) {
for (WebElement checkedBySendKeysElement : elementsToCheckBySendKeys) {
log.info("Checking, that element [" + getLocator(checkedBySendKeysElement) + "] is not clickable by sending keys");
try {
checkedBySendKeysElement.sendKeys("checking");
return false;
} catch (Exception e) {
elementsChecked.add(checkedBySendKeysElement);
}
}
}
return elementsChecked.size() == elements.length;
}