suchi
suchi

Reputation: 1

Checkbox doesn't work properly in selenium

Below steps I need to do:

  1. check if the checkbox is ticked or not.

a)If it is ticked then follow certain steps

b)If it is not ticked then tick the check box and follow the same steps in a)

This is my HTML code:

<td style="width: 4.5%; padding-left: 2px;" class="heading" data-bind="css: selected() ? 'headingSelected' : 'heading'">
    <img title="Click to show all employees" data-bind="click: _apps.expandUnExpand, visible: !expanded()" class="clickable" src="/Content/Images/plus.gif" style="display: none;">
    <img title="Click to hide all employees" data-bind="click: _apps.expandUnExpand, visible: expanded()" class="clickable" src="/Content/Images/minus.png">
    <img title="Click To Select Application" data-bind="click: _apps.selectUnSelect, visible: !selected() &amp;&amp; !_apps.readOnly() " class="clickable" src="/Content/Images/unchecked.png">
    <img title="Click To Unselect Application" data-bind="click: _apps.selectUnSelect, visible: selected() &amp;&amp; !_apps.readOnly() " class="clickable" src="/Content/Images/checked.png" style="display: none;">
</td>

Below is my code:

if(driver.findElement(By.xpath("//[@id='AuraACLs']/div[2]/div[1]/table/tbody/tr/td[1]/img[3]")).isEnabled()) //Xpath when checbox is ticked
{
    System.out.println("checkbox is selected");
    driver.findElement(By.xpath("//button[text()='Add to ACL']")).click();
}
else if(driver.findElement(By.xpath("//[@id='AuraACLs']/div[2]/div[1]/table/tbody/tr/td[1]/img[3]")).isEnabled()) //xpath when checkbox is not ticked
{
    System.out.println("checkbox is unselected");
    driver.findElement(By.xpath("//[@id='AuraACLs']/div[2]/div[1]/table/tbody/tr/td[1]/img[3]")).click();// tick the checkbox
    driver.findElement(By.xpath("//button[text()='Add to ACL']")).click();  
}

If I try with single if the code works but when I combine both the If(if and else if) code doesn't work

Upvotes: 0

Views: 68

Answers (1)

Buster
Buster

Reputation: 715

It looks like your if statements are asking the same question

if (checkbox.isEnabled())
{
  //Do this
}
else if (checkbox.isEnabled())
{
  // Do something else
}

But in both if statements your check condition is the same. Change your else-if to

else if (!checkbox.isEnabled()) The ! is a negation operator and checked that the condition is NOT true.

Upvotes: 1

Related Questions