Title: How do you click on a checkbox from a list of checkboxes via Selenium/Webdriver in Java?
Tags: selenium;webdriver;selenium-webdriver
Question: I'm using Selenium 2 (Webdriver) for automating tests on a webpage. However I wonder if there is way to check checkbox from the list of checkboxes using webdriver framework?
I tried this code but no avail:
IWebElement box = ffDriver.FindElement(By.XPath(&quot;//*[@id='ctl00_ContentPlaceHolder1_Adde_div']&quot;));
List<IWebElement&gt; chkbox = box.FindElements(By.TagName(&quot;input&quot;));
ffDriver.FindElement(By.Id(&quot;ctl00_ContentPlaceHolder1_Add_lstCategory_0&quot;));
//chkbox.g(2).click();

Here is the accepted answer: If you already know the id of the checkbox, you can use this method to click select it:
string checkboxXPath = "//input[contains(@id, 'lstCategory_0')]"
IWebElement elementToClick = driver.FindElement(By.XPath(checkboxXPath));
elementToClick.Click();
Assuming that you have several checkboxes on the page with similar ids, you may need to change 'lstCategory_0' to something more specific.
This is written in C#, but it shouldn't be difficult to adapt to other languages.  Also, if you edit your post with some more information, I can fine-tune this example better.
Let me know if this works!
I've visited the site and successfully interacted with the checkboxes in the dropdown widget using this code:
/** Set XPath Variables **/
string dropdownWidgetXPath = "//span[contains(@id, 'selInd')]";
string checkboxXPath = "//input[contains(@id, 'selInd')]";
/** Navigate to the page **/
driver.Navigate().GoToUrl("http://www.jobserve.com/us/en/Job-Search/");
/** Click the dropdown widget **/
IWebElement dropdownWidgetElement = driver.FindElement(By.XPath(dropdownWidgetXPath));
dropdownWidgetElement.Click();
/** Identify all checkboxes present **/
var allCheckboxes = driver.FindElements(By.XPath(checkboxXPath));
/** Click each checkbox and wait so that results are visible **/
foreach(IWebElement checkbox in allCheckboxes)
checkbox.Click();
System.Threading.Thread.Sleep(500);
Comment for this answer: I use ChromeDriver and your solution with checkbox.Click() does not work. I have to use checkbox.SendKeys(OpenQA.Selenium.Keys.Space); to check or uncheck the checkboxes.
Comment for this answer: if you go to this site http://www.jobserve.com/us/en/Job-Search/ and click on the dropdownlist (Industry) and i am trying to select few check-boxes from there..
Comment for this answer: i tried your code but did not click on the checkbox id which i wanted to be

Here is another answer: By id of the checkbox you could use following code:
IWebElement elementToClick = driver.FindElement(By.ID(ctl00_ContentPlaceHolder1_Add_lstCategory_0));
elementToClick.Click();
If you don't know id then use below code by xpath:
String checkbox = "//input[@type='checkbox']"
IWebElement elementToClick = driver.FindElement(By.XPath(checkbox ));
elementToClick.Click();

Here is another answer: The code in selenium is simple:
new WebDriverWait(driver, TimeSpan.FromSeconds(timeToHoldOn)).Until(ExpectedConditions.ElementExists((By.ClassName("ckb"))));
IWebElement dropdownWidgetElement = driver.FindElement(By.ClassName("ckb"));
dropdownWidgetElement.Click();
Thread.Sleep(1000);
var allCheckboxes = driver.FindElements(By.ClassName("ckb"));
foreach (IWebElement checkbox in allCheckboxes) {
checkbox.Click();
System.Threading.Thread.Sleep(250);

Here is another answer: Try using this piece of code written in java
String checkboxes = "//*[@type='checkbox']";
List<WebElement&gt; elementToClick = driver.findElements(By.xpath(checkboxes));
for (WebElement AllCheck : elementToClick) {
AllCheck.click();

Here is another answer: Java, Clicking on multiple checkboxes at a time using the loop.
**Sample Xpath :**
CheckBox1 Xpath : //input[@class='mycheck' and @id='1']
CheckBox2 Xpath : //input[@class='mycheck' and @id='2']
Get all the elements checkbox using findelements:
List WebElement ele =
driver.findElements(By.xpath("//input[@class='mycheck']"));
Make the Xpath as string by leaving the ID and assign the ID as i.
for(int i=1; i<=ele.size(); i++) { driver.findElement(By.xpath("//input[@class='mycheck' and @id='" +  + i + "']")).click(); }
i gets the value for every loop and the xpath matches the checkbox and click's it.

Here is another answer: You can select each of the radio buttons/checkboxes by selecting the element which contains them and iterating through each one just like an array.
For example, here a ul element contains some radio buttons. I select the ul element first, then I can select each radio by using the correct index (inside the [])
//Select the ul containing the radio buttons I want to click/select
var ul = driver.FindElement(By.Id("ul_containing_radio_buttons"));
//use forloop to click each button in turn
for (var i = 2; i <= 0; i--)
{
var button= ul.FindElements(By.TagName("input"))[i];
//i is the index of the radio button in out ul element
button.Click();
}

Here is another answer: In Selenium webdriver you can do it like this :
All the check-boxes must be having some unique identifier then you can simply find it out by Id If they dont have a unique id (This is what I encountered while testing a web application) then it must be having some title and name attribute (or some other attribute).
Then you can try this :
driver = new FirefoxDriver();
driver.findElement(By.xpath("//input[@name='mycheckboxgroup' and @title='movies']")).click();
driver.findElement(By.xpath("//input[@name='mycheckboxgroup' and @title='songs']")).click();

Here is another answer: This is how I check and uncheck all my boxes, it has to have an Id or class.
Id example:
driver.FindElement(By.Id("someid")).click();
ClassName examaple:
driver.FindElement(By.ClassName("someid")).click();
Its short, its sweet and more importantly it works.

{'source': 'StackExchangeDataset', 'subset': 'stackoverflow.com', 'metadata': {'AnswerCount': 8, 'CommentCount': 0, 'ContentLicense': 'CC BY-SA 4.0', 'CreationDate': '2012:00:08/09/12', 'FavoriteCount': 2.0, 'PostTypeId': 1, 'Tags': '<selenium><webdriver><selenium-webdriver>', 'Title': 'How do you click on a checkbox from a list of checkboxes via Selenium/Webdriver in Java?', 'ViewCount': 81656, 'id': 11888786, 'user_AboutMe': '<p></p>\n', 'user_AccountId': 102635.0, 'user_DisplayName': 'Nick Kahn', 'user_Reputation': 19212.0, 'user_Views': 2824.0, 'user_id': 275390.0}}