WebDriver查找元素不包含属性 [英] WebDriver find Element not containing attribute
问题描述
我有以下HTML
<button class="x-btn-text " style="position: relative; width: 69px;" type="button" tabindex="0" aria-disabled="false">OK</button>
<button class="x-btn-text" style="position: relative; width: 69px;" type="button" tabindex="0">OK</button>
第一个按钮被禁用,第二个按钮被启用,因为我想点击启用的按钮。有没有办法可以找到元素,没有属性 aria-disabled
?
The first button is disabled and the second button is enabled, since I want to click on the enabled button. Is there a way I could find the element, that doesn't have attribute aria-disabled
?
推荐答案
ExtJs案例。在这里,您需要确定是否通过属性 aria-disabled =false
使用 getAttribute(aria-disabled)
或XPath / CssSelector。
ExtJs case. Here you need determine if it's enabled or not by attribute aria-disabled="false"
using getAttribute("aria-disabled")
or XPath/CssSelector.
所以代码热心的逻辑应该是正确的,但是ExtJS总是特别的。
So Code Enthusiastic's logic should be correct, however ExtJS is always special on something.
List<WebElement> okButtons = driver.findElements(By.xpath("//button[text() = 'OK']"));
for (WebElement okButton : okButtons) {
if (!okButton.getAttribute("aria-disabled").equals("false")) {
okButton.click();
break;
}
}
或者更容易,排除您启用的定位。 (因为你需要在这里文本,所以没有合适的定位器使用CssSelector,只有XPath)
Or even easier, rule out the enabled one in your locator. (As you need to text here, so no suitable locator using CssSelector, only XPath)
WebElement enabledokButton = driver.findElement(By.xpath("//button[text() = 'OK' and not(@aria-disabled = 'false')]"));
enabledokButton.click();
这篇关于WebDriver查找元素不包含属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!