This should work for you.
List<WebElement> enabledButtons = driver.findElements(By.xpath("//button[not(@disabled)]"));
if(!enabledButtons.isEmpty()){
enabledButtons.get(0).click();
}
Explanation
Your disabled buttons have disabled
attribute. So you need the xpath that would query all the buttons which do not have that attribute (//button[not(@disabled)]
).
Now you use findElements
method to look up all the elements meeting the xpath criteria. Check if the list is not empty (you have at least one enabled button). And process the very first item.
If you do not need to keep all the enabled buttons you can just use findElement
method instead of findElements
. That one would return the very first element meeting xpath
criteria. However if no elements will be found it will throw NoSuchElementException
and you will have to handle it in some way. Following this assumption your code will be simplified to a one line.
driver.findElement(By.xpath("//button[not(@disabled)][1]")).click();
All said above is relevant to Java language however in terms of algorithm this can be reproduced in any other language Selenium is implemented for.