Skip to main content
added 459 characters in body
Source Link
Alexey R.
  • 11.6k
  • 5
  • 20
  • 39

This should work for you.

List<WebElement> enabledButtons = driver.findElements(By.xpath("//button[not(@disabled)]"));
if(!enabledButtons.isEmpty()){
    enabledButtons.get(0).click();
}

Explanation

YourAccording to your comments, 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.

One-line version

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.

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.

This should work for you.

List<WebElement> enabledButtons = driver.findElements(By.xpath("//button[not(@disabled)]"));
if(!enabledButtons.isEmpty()){
    enabledButtons.get(0).click();
}

Explanation

According to your comments, 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.

One-line version

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.

added 459 characters in body
Source Link
Alexey R.
  • 11.6k
  • 5
  • 20
  • 39

This should work for you.

List<WebElement> enabledButtons = driver.findElements(By.xpath("//button[not(@disabled)]"));
if(!enabledButtons.isEmpty()){
    enabledButtons.get(0).click();
}

ExplanationExplanation

Your disabled buttons have "disabled"disabled attribute. So you need the xpath that would query all the buttons which do not havedo not have that attribute (//button[not(@disabled)]).

Now you use findElementsfindElements 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.

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.

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.

Source Link
Alexey R.
  • 11.6k
  • 5
  • 20
  • 39

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.