title: Selenium WebDriver Java - Clicking on element by label not working on certain labels
body: <p>I am trying to click on element by the label. Here is the code I am using:</p>
  
  <pre><code>driver.findElement(By.xpath("id(//label[text() = 'LABEL TEXT HERE']/@for)")).click();
  </code></pre>
  
  <p>It works for (Select All) &amp; Hayward but cant find Los Angeles, San Fran, or San Jose.</p>
  
  <p>UPDATE:</p>
  
  <p>For now I guess this may be my best option until I see something better.  This will allow the user to pass the full String and the function in the method will grab the last word of the string and insert it into the contains xpath.  </p>
  
  <pre><code>public void subStringLocationTest(String location) {
  
      String par = location.substring(location.lastIndexOf(" ") + 1);
  
      driver.findElement(By.xpath("//label[contains(text(), '" + par + "')]")).click();
  
  }
  </code></pre>
  
  <p>Here is the HTML code:</p>
  
  <pre><code>&lt;div id="ReportViewer1_ctl04_ctl03_divDropDown" onclick="event.cancelBubble=true;" onactivate="event.cancelBubble=true;" style="border: 1px solid rgb(169, 169, 169); font-family: Verdana; font-size: 8pt; overflow: auto; background-color: window; display: inline; position: absolute; z-index: 11; left: 131px; top: 41px; width: 188px;"&gt;
      &lt;span&gt;&lt;table cellpadding="0" cellspacing="0" style="background-color:window;"&gt;
          &lt;tbody&gt;&lt;tr&gt;
              &lt;td nowrap="nowrap"&gt;&lt;span style="font-family:Verdana;font-size:8pt;"&gt;&lt;input id="ReportViewer1_ctl04_ctl03_divDropDown_ctl00" type="checkbox" name="ReportViewer1$ctl04$ctl03$divDropDown$ctl00" onclick="$get('ReportViewer1_ctl04_ctl03').control.OnSelectAllClick(this);"&gt;&lt;label for="ReportViewer1_ctl04_ctl03_divDropDown_ctl00"&gt;(Select All)&lt;/label&gt;&lt;/span&gt;&lt;/td&gt;
          &lt;/tr&gt;&lt;tr&gt;
              &lt;td nowrap="nowrap"&gt;&lt;span style="font-family:Verdana;font-size:8pt;"&gt;&lt;input id="ReportViewer1_ctl04_ctl03_divDropDown_ctl02" type="checkbox" name="ReportViewer1$ctl04$ctl03$divDropDown$ctl02" onclick="$get('ReportViewer1_ctl04_ctl03').control.OnValidValueClick(this, 'ReportViewer1_ctl04_ctl03_divDropDown_ctl00');"&gt;&lt;label for="ReportViewer1_ctl04_ctl03_divDropDown_ctl02"&gt;Hayward&lt;/label&gt;&lt;/span&gt;&lt;/td&gt;
          &lt;/tr&gt;&lt;tr&gt;
              &lt;td nowrap="nowrap"&gt;&lt;span style="font-family:Verdana;font-size:8pt;"&gt;&lt;input id="ReportViewer1_ctl04_ctl03_divDropDown_ctl03" type="checkbox" name="ReportViewer1$ctl04$ctl03$divDropDown$ctl03" onclick="$get('ReportViewer1_ctl04_ctl03').control.OnValidValueClick(this, 'ReportViewer1_ctl04_ctl03_divDropDown_ctl00');"&gt;&lt;label for="ReportViewer1_ctl04_ctl03_divDropDown_ctl03"&gt;Los Angeles&lt;/label&gt;&lt;/span&gt;&lt;/td&gt;
          &lt;/tr&gt;&lt;tr&gt;
              &lt;td nowrap="nowrap"&gt;&lt;span style="font-family:Verdana;font-size:8pt;"&gt;&lt;input id="ReportViewer1_ctl04_ctl03_divDropDown_ctl04" type="checkbox" name="ReportViewer1$ctl04$ctl03$divDropDown$ctl04" onclick="$get('ReportViewer1_ctl04_ctl03').control.OnValidValueClick(this, 'ReportViewer1_ctl04_ctl03_divDropDown_ctl00');"&gt;&lt;label for="ReportViewer1_ctl04_ctl03_divDropDown_ctl04"&gt;San Francisco&lt;/label&gt;&lt;/span&gt;&lt;/td&gt;
          &lt;/tr&gt;&lt;tr&gt;
              &lt;td nowrap="nowrap"&gt;&lt;span style="font-family:Verdana;font-size:8pt;"&gt;&lt;input id="ReportViewer1_ctl04_ctl03_divDropDown_ctl05" type="checkbox" name="ReportViewer1$ctl04$ctl03$divDropDown$ctl05" onclick="$get('ReportViewer1_ctl04_ctl03').control.OnValidValueClick(this, 'ReportViewer1_ctl04_ctl03_divDropDown_ctl00');"&gt;&lt;label for="ReportViewer1_ctl04_ctl03_divDropDown_ctl05"&gt;San Jose&lt;/label&gt;&lt;/span&gt;&lt;/td&gt;
          &lt;/tr&gt;
      &lt;/tbody&gt;&lt;/table&gt;&lt;input type="hidden" name="ReportViewer1$ctl04$ctl03$divDropDown$ctl01$HiddenIndices" id="ReportViewer1_ctl04_ctl03_divDropDown_ctl01_HiddenIndices" value=""&gt;&lt;/span&gt;
  &lt;/div&gt;
  </code></pre>