HillHacker HillHacker - 11 days ago 6
Java Question

How to check all check-boxes one by one in selenium web driver with the java?

I am using the below code , The test case is not failing but the code is not checking the check-boxes.

@Test(priority=11)
public void Test_CheckBox_Check()throws InterruptedException {

List<WebElement> els = driver.findElements(By.xpath("//md-checkbox[@aria-checked='false']"));
System.out.println(Integer.toString(els.size()));
for ( WebElement el : els ) {

el.click();

}
}


Test Case is not failing
Xptha and the elements

enter image description here

Answer

The locator you are using might be the causing the issue try with below:

//div[@class='ng-scope flex-20']//following::md-checkbox**[@role='checkbox']**

You may omit the part in asterisk if all the elements that are identified by md-checkbox are checkboxes.

Below code worked for me in another case:

List<WebElement> checkboxes = driver.findElements(By.xpath("//div[@class='control-group']//following::input[@type='checkbox']"));
        for(WebElement check:checkboxes){
            check.click();
        }