我有以下 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
的元素?
最佳答案
ExtJs 案例。在这里,您需要使用 getAttribute("aria-disabled")
或 XPath/CssSelector 确定属性 aria-disabled="false"
是否启用。
所以Code Enthusiastic的逻辑应该是正确的,但是ExtJS总是在某些方面很特别。
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)
WebElement enabledokButton = driver.findElement(By.xpath("//button[text() = 'OK' and not(@aria-disabled = 'false')]"));
enabledokButton.click();
关于java - WebDriver 发现元素不包含属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18770486/