How to Check All the Checkboxes Present in a Page Using Selenium


This post tells you how to check all the checkboxes present in the page.

import java.util.List;
import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.annotations.Test;

public class testng_3 {
@Test
public void checkbox() throws Exception
{
FirefoxDriver fd= new FirefoxDriver();
fd.manage().window().maximize();
fd.get(“http://www.flipkart.com/laptops/lenovo~brand/pr?sid=6bo,b5g&otracker=hp_nmenu_sub_electronics_0_Lenovo”);
Thread.sleep(10000);
List checkBoxes = fd.findElements(By.xpath(“//input[@type=’Checkbox’]”));
System.out.println(checkBoxes.size());
for(int i=0; i<checkBoxes.size(); i++){
System.out.println(checkBoxes.get(i).getText());
checkBoxes.get(i).click();
}
fd.close();
}
}

Advertisements

2 thoughts on “How to Check All the Checkboxes Present in a Page Using Selenium

    • Parvez shaikh September 19, 2017 / 1:49 am

      tried with your but it is not working on flipkart
      My code is :::—

      package selone;

      import java.util.List;
      import java.util.concurrent.TimeUnit;

      import org.openqa.selenium.By;
      import org.openqa.selenium.WebDriver;
      import org.openqa.selenium.firefox.FirefoxDriver;

      public class DslrBrandChecboxes {

      public static void main(String[] args) throws InterruptedException {
      WebDriver driver = new FirefoxDriver();

      driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
      driver.manage().window().maximize();
      driver.get(“https://www.flipkart.com/cameras/dslr~type/pr?count=40&otracker=nmenu_sub_Electronics_0_DSLR&sid=jek%2Fp31”);

      Thread.sleep(10000);

      List checkBoxes = driver.findElements(By.xpath(“//input[@type=’Checkbox’]”));
      System.out.println(checkBoxes.size());

      }

      }

      Like

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s