The following code snippet represents a checkbox
and I want to check whether it is checked or not using Selenium.
for (int j = 2; j < 5; j++)
{
List<WebElement> td1 = row1.findElements(By.tagName("td"));
WebElement check = td1.get(j);
if (check.isEnabled())
{
if (!check.isSelected())
{
List<WebElement> checkBox = ele
.findElements(By.xpath("//input[contains(@ng-change,'vm.updateInvoicingMethodMailByGroup(item)')]"));
WebElement elem3 = checkBox.get(count);
elem3.click();
count++;
break;
}
}
}
When I check this, the class changes from
class="ng-valid ng-dirty ng-valid-parse ng-empty ng-touched"
to
class="ng-valid ng-dirty ng-valid-parse ng-not-empty ng-touched"
HTML
<input type="checkbox" name="invoicingByMailFlag" ng-model="item.invoicingByMailFlag"
ng-change="vm.updateInvoicingMethodMailByGroup(item)" class="ng-valid ng-dirty
ng-valid-parse ng-empty ng-touched">`
You can check if it has class attribute ng-empty
or not
List<WebElement> checkBoxes = ele.findElements(By.xpath("//input[contains(@ng-change,'vm.updateInvoicingMethodMailByGroup(item)')]"));
WebElement checkBox = checkBoxes.get(count);
if (checkBox.getAttribute("class").contains("ng-empty")) {
// checkbox not checked
}
If your requirement to check the unchecked boxes then try below code :
List <WebElement> allcheckbox = driver.findElements(By.xpath("//input[@type='checkbox']"));
for(WebElement element : allcheckbox)
{
if(!(element.isSelected()))
{
element.click();
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.