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

Leave a Reply

Fill in your details below or click an icon to log in:

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