I have a problem and having trouble to finding a solution.

Problem Statement:

Navigate to a webpage and check if three checkboxes are selected. If not, I'd like to select them (if only one is selected, I'd like to select the other two and etc). If they are already selected, then exit the page.

I have declared these 3 elements with xpath as below :

IWebElement aaa = driver.findElement....

IWebElement bbb = driver.findElement....

IWebElement ccc = driver.findElement....

I would like to wrap the above elements into a variable perhaps to do something like this:

var threeCheckboxes = aaa, bbb, ccc

So I could get a potential conditional statement like:

if (threeCheckboxes.Selected){
   select the three boxes(etc)
  • What is the relevant html source of these checkboxes on page? Sep 9, 2018 at 12:52

1 Answer 1


I am not a C# guy, I can help you with a basic idea how you can achieve this with pseudo-code, then you can translate the same in C#.

Add all the check boxes to list. Loop thru the list to check which of them is not selected. If not selected, perform a click to select that checkbox.

    List<WebElement> _all_check_box = new ArrayList<WebElement>();

    for (WebElement w : _all_check_box)
        if (!w.isSelected())  //this may be w.isChecked() or w.Checked()
  • This was pretty close - I'm not sure of the actual calls for C# selenium, but I "C#-ified" your code, and your logic is spot on.
    – Kate Paulk
    Sep 6, 2018 at 19:55
  • Thank you for your suggestion! I am terribly sorry but I am a bit new at this and am having the following issue and need clarification: IWebElement aaa = driver.FindElement... IWebElement bbb = driver.FindElement... IWebElement ccc = driver.FindElement... IList<IWebElement> checkboxList = new List<IWebElement>(); checkboxList.Add(aaa); checkboxList.Add(bbb); checkboxList.Add(ccc); for (IWebElement cboxList = checkboxList) "for (IWebElement cboxList = checkboxList)" is throwing an error (missing a cast). Sep 6, 2018 at 20:01
  • After messing around, I got the following: foreach (IWebElement cboxList in checkboxList) { if (!aaa.Selected) { aaa.Click(); } if (!bbb.Selected) { bbb.Click(); } if (!ccc.Selected) { ccc.Click(); } } Sep 6, 2018 at 21:24
  • You already have an iterator in the foreach loop. There is no point in explicitly using the WebElement inside the loop which makes the list iterator of no use. Instead do: foreach (IWebElement cboxList in checkboxList) { if (!cboxList.Selected) { cboxList.Click(); } Sep 7, 2018 at 5:13
  • Okay great, I did that. Thank you so much and it worked. One final issue: with the solution, what happens is, the page loads and IF the checkboxes are already checked, they get unchecked! When the opposite is supposed to happen. I.e if the checkboxes are not checked, check them. Perhaps I need to have the statement the other way around? I.e if (cboxList.Selected) [proceed to the rest of the steps] else cboxList.Click(); Sep 7, 2018 at 14:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.