title: How to click on checkboxes based on label text
body: <p>I'm trying to use xPath to click on the yes checkbox but I can't seem to figure it out. I have tried </p>
  
  <pre><code>driver.find_element_by_xpath(".//label[following-sibling::input[contains(., 'Yes')]]").click() .
  </code></pre>
  
  <p>but this didn't work. 
  In the past I have used         </p>
  
  <pre><code>driver.find_element_by_xpath("//input[@id=(//label[text()=\"Phone Number\"]/@for)]")
  </code></pre>
  
  <p>to find text fields but I haven't been able to do the same for the checkbox.
   Any help would be much appreciated. </p>
  
  <pre class="lang-html prettyprint-override"><code>&lt;dl data-validation="{&amp;quot;checkboxes&amp;quot;:
  {&amp;quot;required&amp;quot;:true,&amp;quot;and&amp;quot;:
  [{&amp;quot;name&amp;quot;:&amp;quot;checkboxes&amp;quot;,&amp;quot;operator&amp;quot;:&amp;quot;checkboxes&amp;quot;,&amp;quot;options&amp;quot;:{&amp;quot;message&amp;quot;:&amp;quot;must select a value&amp;quot;}}]}}" class="form"&gt;
    &lt;dt&gt;&lt;div class="form_response_label required" id="responses_2865807_label"&gt;Are you an Undergraduate Student?&lt;/div&gt;&lt;/dt&gt;
  &lt;input id="responses_2865807_" name="responses[2865807][]" type="hidden"&gt;
    &lt;div class="form-checkbox"&gt;
      &lt;label for="responses_2865807_4737821"&gt;
        &lt;input id="responses_2865807_4737821" name="responses[2865807][]" type="checkbox" value="4737821"&gt;
        Yes
  &lt;/label&gt;      &lt;/div&gt;
    &lt;div class="form-checkbox"&gt;
      &lt;label for="responses_2865807_4737822"&gt;
        &lt;input id="responses_2865807_4737822" name="responses[2865807][]" type="checkbox" value="4737822"&gt;
        No
  &lt;/label&gt;      &lt;/div&gt;
  &lt;small&gt;&lt;span class="right char_count"&gt;&lt;/span&gt;&lt;/small&gt;&lt;div class="form-constraints"&gt;&lt;div class="validation-summary" style="display: none;"&gt;&lt;strong&gt;Validation&lt;/strong&gt;&lt;div class="explanation"&gt;Valid input may include: must select a value&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;&lt;/dl&gt;
  </code></pre>
  
  <p>My main focus is checking yes in the following code:</p>
  
  <pre class="lang-html prettyprint-override"><code>&lt;label for="responses_2865807_4737821"&gt;
        &lt;input id="responses_2865807_4737821" name="responses[2865807][]" type="checkbox" value="4737821"&gt;
  "
        Yes
  "
  &lt;/label&gt;
  </code></pre>
  
  <p><strong>Now that I figured out how to find the check that says "Yes", how would I be able to select "Yes" specifying the label that came before it, in case I get into a situation where i need to change between Yes and No depending on the question?</strong> 
  So in the example code above I would clarify that I am looking for the "Yes" checkbox labeled by "Are you an Undergraduate Student?" </p>