Search code examples
javaseleniumbuttonusing

Single button used for enable/disable user how to identify button status using Selenium Webdriver


Here single button is used for enable/disable user, when you click on the button and if the button is in disabled mode then it becomes enable (user becomes active) or if the button is in enabled mode then it becomes disable (user becomes inactive) Here my concern is how can i get the status of the button is in what status so i can perform operation based upon the status of button.

Here is code snippet for the same..

When button is in enabled mode.

<a id="13" class="btn btn-xs btn-success inactive" title="Click to Inactivate" href="javascript:void(0);" data-active-inactive="inactive">
<span class="ion-checkmark"></span>
</a>

When button is in Disabled mode.

<a id="13" class="btn btn-xs btn-danger active" title="Click to Active" href="javascript:void(0);" data-active-inactive="active">
<span class="ion-android-close"></span>
</a>

Here is Image of the front-end. enter image description here


Solution

  • you get the attributes of the element then check it like:

    Python:

    elm = driver.find_element_by_id("13")
    class = elm.get_attribute("class")
    if "inactive" in class:
        print "button is disabled"
    else:
        print "button is enabled"    
    

    Java:

    Webelement elm = driver.findElement(By.id("13"));
    String c_elm = elm.getAttribute("class")
    if c_elm.contains("inactive"){
        System.out.println("button is disabled")
    }
    else{
        System.out.println("button is enabled")
    }