Bhupendra Singh Rautela
Bhupendra Singh Rautela

Reputation: 3596

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

Upvotes: 0

Views: 1694

Answers (2)

Bhupendra Singh Rautela
Bhupendra Singh Rautela

Reputation: 3596

After adding some wait its working

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

    Thread.sleep(2000);

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

        Thread.sleep(2000);

        el.click();

         System.out.println(el.getText());
         driver.findElement(By.xpath("//div[@class='col-xs-1']")).click();

    }

Upvotes: 0

SelThroughJava
SelThroughJava

Reputation: 331

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();
        }

Upvotes: 1

Related Questions