//How to count the number of checkboxes checked in selenium webdriver ? or How to select multiple check box and verify ?
import java.util.List;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
public class Count_Checkbox_Checked {
public static void main(String[] args) {
WebDriver driver = new FirefoxDriver();
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
driver.get("http://www.gsmarena.com/samsung-phones-9.php");
List<WebElement> checkBoxes = driver.findElements(By.xpath("//input[@type='Checkbox']"));
// for(int i=0; i<checkBoxes.size(); i=i+2){
for(int i=0; i<checkBoxes.size(); i=i+4)
{
checkBoxes.get(i).click();
}
int checkedCount=0, uncheckedCount=0;
for(int i=0; i<checkBoxes.size(); i++){
System.out.println(i+" checkbox is selected "+checkBoxes.get(i).isSelected());
if(checkBoxes.get(i).isSelected()){
checkedCount++;
}else{
uncheckedCount++;
}
}
System.out.println("number of selected checkbox: "+checkedCount);
System.out.println("number of unselected checkbox: "+uncheckedCount);
}
}
import java.util.List;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
public class Count_Checkbox_Checked {
public static void main(String[] args) {
WebDriver driver = new FirefoxDriver();
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
driver.get("http://www.gsmarena.com/samsung-phones-9.php");
List<WebElement> checkBoxes = driver.findElements(By.xpath("//input[@type='Checkbox']"));
// for(int i=0; i<checkBoxes.size(); i=i+2){
for(int i=0; i<checkBoxes.size(); i=i+4)
{
checkBoxes.get(i).click();
}
int checkedCount=0, uncheckedCount=0;
for(int i=0; i<checkBoxes.size(); i++){
System.out.println(i+" checkbox is selected "+checkBoxes.get(i).isSelected());
if(checkBoxes.get(i).isSelected()){
checkedCount++;
}else{
uncheckedCount++;
}
}
System.out.println("number of selected checkbox: "+checkedCount);
System.out.println("number of unselected checkbox: "+uncheckedCount);
}
}
No comments:
Post a Comment