title: Selenium Webdriver - Unticking all the chekboxes in a complicated table
body: <p>I am having some problems currently trying to untick checkboxes in an iframe. The situation is it is currently possible to set some checkboxes to default ticked and some not. I need 1 specific checkbox ticked, so the sensible thing to do is run a loop that iterates through all the checkboxes and unchecks them all.</p>
  
  <p>Here is where I am running into issues. I will post a sample of the HTML that the checkboxes are contained in. (This isn't mine so I can't edit the HTML unfortunately).</p>
  
  <p>This is how the example looks in a situation where there are 3 different types of checkbox in the iframe.</p>
  
  <pre><code>&lt;fieldset id="testing"&gt;
  &lt;legend&gt;testing&lt;/legend&gt;
  &lt;table&gt;
      &lt;tbody&gt;
          &lt;tr&gt;
              &lt;td class="EXAMPLE"&gt;
                  &lt;table id="CHECKBOXTYPE1"&gt;
                      &lt;tbody&gt;
                          &lt;tr&gt;
                              &lt;td style="vertical-align:top;white-space:nowrap;" title=""&gt;
                                  &lt;input id="CHECKBOXTYPE1-01" type="checkbox" value="on" onclick="DOES STUFF;"/&gt;
                              &lt;/td&gt;
                              &lt;td style="vertical-align:top;white-space:nowrap;" title=""&gt;TITLE1&lt;/td&gt;
                          &lt;/tr&gt;
                          &lt;tr&gt;
                              &lt;td/&gt;
                              &lt;td id="CHECKBOXTYPE2-01" style="display:none;white-space:nowrap;vertical-align:top;padding:0px;"&gt;
                                  &lt;table&gt;
                                      &lt;tbody&gt;
                                          &lt;tr&gt;
                                              &lt;td style="vertical-align:top;" colspan="3"&gt;
                                                  &lt;select id="field" style="width:100%;"&gt;
                                                      &lt;option value="1"&gt;STUFF1 &lt;/option&gt;
                                                      &lt;option value="2"&gt;STUFF2 &lt;/option&gt;
                                                      &lt;option value="3"&gt;STUFF3 &lt;/option&gt;
                                                      &lt;option value="4"&gt;STUFF4 &lt;/option&gt;
                                                  &lt;/select&gt;
                                              &lt;/td&gt;
                                              &lt;td style="vertical-align:bottom;padding-left:6px;" rowspan="2"&gt;
                                                  &lt;textarea id="CHECKBOXTYPE2-01-COMMENTS" cols="50" rows="2" style="margin:0px;height:50px;" type="text" onclick="DOES STUFF"&gt;Please Insert Notes...&lt;/textarea&gt;
                                              &lt;/td&gt;
                                          &lt;/tr&gt;
                                          &lt;tr&gt;
                                              &lt;td style="vertical-align:bottom;"&gt;
                                                  &lt;input type="CHECKBOXTYPE2-01-BUTTON" onclick="DOES STUFF" value="&lt;" style="height:100%;width:32px;"/&gt;
                                              &lt;/td&gt;
                                              &lt;td style="vertical-align:bottom;"&gt;
                                                  &lt;input id="CHECKBOXTYPE2-01-INPUT" type="input" readonly="" style="width:112px;"/&gt;
                                              &lt;/td&gt;
                                              &lt;td style="vertical-align:bottom;"&gt;
                                                  &lt;input type="CHECKBOXTYPE2-01-BUTTON" onclick="DOES STUFF" style="width:32px;height:100%;" value="&gt;"/&gt;
                                              &lt;/td&gt;
                                          &lt;/tr&gt;
                                      &lt;/tbody&gt;    
                                  &lt;/table&gt;
                              &lt;/td&gt;
                          &lt;/tr&gt;
                      &lt;/tbody&gt;
                  &lt;/table&gt;
              &lt;/td&gt;
          &lt;/tr&gt;
          &lt;tr&gt;
              &lt;td class="field_label"&gt;
                  &lt;table id="CHECKBOXTYPE3"&gt;
                      &lt;tbody&gt;
                          &lt;tr&gt;
                              &lt;td style="vertical-align:top;"&gt;
                                  &lt;input id="CHECKBOXTYPE3-01" type="checkbox" title="" onclick="DOES STUFF"/&gt;
                              &lt;/td&gt;
                              &lt;td style="vertical-align:top;" title=""&gt;CHECKBOX NAME&lt;/td&gt;
                              &lt;td style="vertical-align:top;"&gt;
                          &lt;/tr&gt;
                      &lt;/tbody&gt;
                  &lt;/table&gt;
              &lt;/td&gt;
          &lt;/tr&gt;
      &lt;/tbody&gt;
  &lt;/table&gt;
  </code></pre>
  
  <p></p>
  
  <p>The code I have attempted to iterate is</p>
  
  <pre><code>try{
              for(int i=0; i &lt; 30; i++){
                  WebElement relCheckBoxes = driver.findElement(By.xpath("html/body/div[3]/fieldset/table/tbody/tr[i]/td/table/tbody/tr/td[1]"));
                  if(relCheckBoxes.isSelected()){
                      relCheckBoxes.click();
                  }               
              }
          }
          catch(Exception e){
              System.out.printf("didn't work");
          }
  </code></pre>
  
  <p>Obviously this is not the most optimised piece of code, but right now I'm just struggling to find something that works :\ I just want to run through the checkboxes, turn off all of them, then turn on the one that I need after. </p>
  
  <p>Thank you.</p>