2

enter image description here

I am currently changing my XPath every time to click on the enable (Qualification check) button. However, I want to click on the first button that is in the enabled state. I have a total of 10 buttons out of which some are enabled and some are disabled. How can I find and click the first enabled button?

HTML element code: Disabled element

<div>
    <button type="button" id="btnQualificationCheck_3" 
            style="padding-left:15px; padding-right:15px;" 
            class="btn btn-primary btn-sm btn-fill qualifier" 
            disabled="">Qualification Check</button>
</div>

Enabled element

<div>
    <button type="button" id="btnQualificationCheck_4" 
            style="padding-left:15px; padding-right:15px;" 
            class="btn btn-primary btn-sm btn-fill qualifier">Qualification Check</button>
</div>
4
  • Share your html. How is it defined whether the button is enabled or disabled? Is there any attribute or a special style assigned?
    – Alexey R.
    Commented May 18, 2018 at 9:11
  • HTML element code are : <div> <button type="button" id="btnQualificationCheck_3" style="padding-left:15px; padding-right:15px;" class="btn btn-primary btn-sm btn-fill qualifier" disabled="">Qualification Check</button> </div> <div> <button type="button" id="btnQualificationCheck_4" style="padding-left:15px; padding-right:15px;" class="btn btn-primary btn-sm btn-fill qualifier">Qualification Check</button> </div> Commented May 18, 2018 at 9:53
  • The first one represents disabled button and the second one is enabled button?
    – Alexey R.
    Commented May 18, 2018 at 9:55
  • yes, Fir Disable the class code is tagged by disable and for enabling of button nothing is done.Like wise their are more than 100 button , i just want to click the first enable button and bypass the disable button Commented May 18, 2018 at 9:57

3 Answers 3

3

You can try the isEnabled() function for checking if the element is enabled or not. The function will return 1 if the WebElement is enabled, else it will return 0.

WebElement object1 = driver.findElement(By.id("ID1"));
WebElement object2 = driver.findElement(By.id("ID2"));
if (object1.isEnabled()()){
    object1.click();
}else if (object2.isEnabled()){
          object2.click();
         }

Similarly, you can do this for 10 elements.

If the button's position is dynamic, then you can use the following code.

List <WebElement> buttons = driver.findElements(By.linkText("Qualification Check"));
for ( i=0; i<buttons.size();i++){
    WebElement button = buttons.get(i);
    if button.isEnabled(){
        button.click();
        }         
    }
4
  • This doesn't look well because you hardcode button order (in your if-else construction). In your code you say that button with ID1 is assumed to go before button with ID2 however this is not necessarily true.
    – Alexey R.
    Commented May 18, 2018 at 9:31
  • Are you saying that the button's position is dynamic and changes frequently?? Then, Yes, this may be a problem....If the positions of the buttons are fixed, then this code may work..
    – Rishi
    Commented May 18, 2018 at 9:55
  • Yes the button's position is dynamic and changes frequently . So do you know how can we handle this ? Commented May 18, 2018 at 10:00
  • I have posted the code in the answer for buttons with dynamic positions.Check if this works
    – Rishi
    Commented May 18, 2018 at 10:14
1

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.

1

The button[id*='btnQualification'] find all Qulification Check button from all buttons in page.

The :not(:disabled) filter out those disabled button.

Combine both we get enabled Qulification Check button.

String locator = "button[id*='btnQualification']:not(:disabled)";
driver.findElement(By.cssSelector(locator)).click();

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.