How do you click on a checkbox from a list of checkboxes via Selenium/Webdriver in Java?

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

Leave a Comment