How to Tell If a Checkbox Is Selected in Selenium for Java

How do you tell if a checkbox is selected in Selenium for Java?

If you are using Webdriver then the item you are looking for is Selected.

Often times in the render of the checkbox doesn't actually apply the attribute checked unless specified.

So what you would look for in Selenium Webdriver is this

isChecked = e.findElement(By.tagName("input")).Selected;

As there is no Selected in WebDriver Java API, the above code should be as follows:

isChecked = e.findElement(By.tagName("input")).isSelected();

Check if a checkbox is checked or not using Selenium

You can check if it has class attribute ng-empty or not

List<WebElement> checkBoxes = ele.findElements(By.xpath("//input[contains(@ng-change,'vm.u‌​pdateInvoicingMethod‌​MailByGroup(item)')]‌​"));
WebElement checkBox = checkBoxes.get(count);
if (checkBox.getAttribute("class").contains("ng-empty")) {
// checkbox not checked
}

selenium webdriver validate if checkbox is checked

You can create method which will convert Your table to Java Map for example:

public static Map<String,String> getTableAsMap(WebDriver driver)
{
Map<String,String> checkboxMap = new TreeMap<>();

List<WebElement> header = driver.findElements(By.xpath("((//tbody/tr)[1])/td/*"));
List<WebElement> checkboxes = driver.findElements(By.xpath("((//tbody/tr)[2])/td/*"));

for(int i = 0;i<header.size();i++){

//Only for testing purpose
System.out.printf("KEY: %s, VALUE: %s\n",header.get(i).getText(),checkboxes.get(i).getAttribute("disabled"));

checkboxMap.put(
header.get(i).getText(),
checkboxes.get(i).getAttribute("disabled")
);
}
return checkboxMap;
}

Then In your main class with driver use:

Webdriver driver = new FirefoxDriver();
Map<String,String> map = getTableAsMap(driver);

System.out.println(map.get("Fri"));
System.out.println(map.get("Sat"));

For disabled (non selected) checkbox You will receive true for selected there will be null. Both as String.

How can I check the checkbox state using Selenium?

As per the HTML you have shared, it's clear that the <span> tag of the outerHTML of the Checkbox contains the following class attributes:

  • Checked

     <span id="messageGrid_DXSelBtn1_D" class="dxICheckBox_MetropolisBlue dxichSys dxWeb_edtCheckBoxChecked_MetropolisBlue">
    <input id="messageGrid_DXSelBtn1" value="U" readonly="readonly" style="border-width:0;width:0;height:0;padding:0;margin:0;position:relative;background-color:transparent;display:block;" type="text"/>
    </span>
  • Unchecked

     <span id="messageGrid_DXSelBtn1_D" class="dxWeb_edtCheckBoxUnchecked_MetropolisBlue dxICheckBox_MetropolisBlue dxichSys">
    <input id="messageGrid_DXSelBtn1" value="U" readonly="readonly" style="border-width:0;width:0;height:0;padding:0;margin:0;position:relative;background-color:transparent;display:block;" type="text"/>
    </span>

So to check if the Check Box is checked or unchecked you can induce the following validation:

if(driver.FindElement(By.XPath("//input[@id='messageGrid_DXSelBtn1']//preceding::span[1]")).GetAttribute("class").contains("dxWeb_edtCheckBoxChecked_MetropolisBlue"))
Console.WriteLine("Check Box is Checked");
else
Console.WriteLine("Check Box is Unchecked");

can't verify selected checkbox, Selenium Webdriver Java

To be able to use isSelected method, you have to address to the input type="checkbox" element.

Use css selector #main-citizenship-checkbox-0 input for the first checkbox.

Probably for the second one, it will be #main-citizenship-checkbox-1 input.

@FindBy(css = "#main-citizenship-checkbox-0 input")
@CacheLookup
public WebElement checkboxForFirstCitizenship;


Related Topics



Leave a reply



Submit