0

I am trying to get the status of the checkbox, whether checked or not in selenium using java. Below is the code of the html and the code i have written:

HTML Code::

<div class="icheckbox_square-aero" style="position: relative;" aria-checked="false" aria-disabled="false">

<input id="IAgree" class="icheckbox_square-aero" type="checkbox" required="" name="IAgree" style="position: absolute; top: -20%; left: -20%; display: block; width: 140%; height: 140%; margin: 0px; padding: 0px; background: rgb(255, 255, 255) none repeat scroll 0% 0%; border: 0px none; opacity: 0;">

<ins class="iCheck-helper" style="position: absolute; top: -20%; left: -20%; display: block; width: 140%; height: 140%; margin: 0px; padding: 0px; background: rgb(255, 255, 255) none repeat scroll 0% 0%; border: 0px none; opacity: 0;"></ins>
</div>

Programming code::

String loc_iAgree  = pr.getProperty("iagree_xpath"); 
//iagree_xpath is declared in properties file as "iagree_xpath = //form[@id='myWizard']/div/section[3]/div[3]/div/ins"
uia.clickOnCheckbox("xpath", loc_iAgree);

code for clickOnCheckbox() method::

    public void clickOnCheckbox(String loc_Type, String loc_Value) {
    try {
        if (loc_Type.equalsIgnoreCase("id")) {
            element = driver.findElement(By.id(loc_Value));
        } else if (loc_Type.equalsIgnoreCase("name")) {
            element = driver.findElement(By.name(loc_Value));
        } else if (loc_Type.equalsIgnoreCase("xpath")) {
            element = driver.findElement(By.xpath(loc_Value));
        } else if (loc_Type.equalsIgnoreCase("linktext")) {
            element = driver.findElement(By.linkText(loc_Value));
        } else if (loc_Type.equalsIgnoreCase("partiallinktext")) {
            element = driver.findElement(By.partialLinkText(loc_Value));
        } else if (loc_Type.equalsIgnoreCase("cssSelector")) {
            element = driver.findElement(By.cssSelector(loc_Value));
        } else {
            System.out.println("provide valid locator Type");
        }

        String value;
        if (element.isEnabled()) {
            element.click();
            value = element.getAttribute("checked");
            System.out.println(value);
        } else {
            System.out.println("element is not present or not enabled");
        }
    } catch (Exception e) {
        throw (e);
    }
}

Here the value returns null even if the checkbox is getting checked.

Is there any other way to get the status of the checkbox back to the calling program??

Prathi
  • 1
  • 1
  • 3
  • Try using [`.isSelected()`](https://seleniumhq.github.io/selenium/docs/api/java/org/openqa/selenium/WebElement.html#isSelected--) instead of `isEnabled()`. – JRodDynamite Jun 29 '16 at 06:15

1 Answers1

0

isSelected() in Selenium WebDriver Link

Gaurav Lad
  • 1,788
  • 1
  • 16
  • 30