title: Improvement With Xpath Search?
body: <p>I am a student learning selenium and trying to set a checkbox(by placing a tick) that resides on the same row as a target document.  Here is how the row looks:</p>
  
  <pre><code>            &lt;span&gt;&lt;input&gt;Checkbox&lt;/input&gt;&lt;/span&gt; - &lt;span&gt;_lblDocumentCategoryDescription&lt;/span&gt; - &lt;span&gt;_lblDocumentdescription&lt;/span&gt;
  </code></pre>
  
  <p>Each row consists of a checkbox for that row, a category and a description.  I am provided the description and need to set the corresponding checkbox at the beginning of the row.  </p>
  
  <p>Although I have a solution that works I am looking for a better method as my method is very slow and I can call it multiple times, compounding the slowness.  The page in question has 315 fixed different documents that might possibly be added (to a merge and print feature). I usually add at least 3 rows so need to run the search    </p>
  
  <p>I am currently using xpath to search since my knowledge of CSS is limited and it seems like traversing up the DOM (no :parent?) is not easy unless using some javascript, which I am trying to avoid.  I have read that executing JS doesn't emulate the user correctly so I guess xpath is the solution?</p>
  
  <p>As I know there must be a quicker/better/more efficient solution I am hoping others might have found better?</p>
  
  <p>Here is what I am currently using:</p>
  
  <p>Method Call
                  CPTools,setStupidCheckbox("Sublease Addendum", true);</p>
  
  <p>Method:</p>
  
  <pre><code>            public CPTools setStupidCheckbox(String documentToPrint, Boolean onIfTrue) {
                      this.log.trace("Starting: setStupidCheckbox; parameter: documentToPrint " + documentToPrint);
                      String xpathForCheckboxes = "/ancestor::tr//input";
                      try {
                          String xpathForDocumentDescriptions = String.format("//span[contains(text(), '%s')]", documentToPrint);
                          tools.waitForElement(By.xpath(xpathForDocumentDescriptions), 2);
                          if (driver.findElement(By.xpath(xpathForDocumentDescriptions)).isDisplayed()) {
                              driver.findElementByXPath(xpathForDocumentDescriptions + xpathForCheckboxes).click();
                          }
                      } catch (org.openqa.selenium.NoSuchElementException | ElementNotVisibleException e) {
                      }
                      this.log.trace("Ending: setStupidCheckbox");
                      return this;
                  }
  </code></pre>
  
  <p>Here is the full xpath for a table row (the dynamic part of the id is the _ctl316):</p>
  
  <pre><code>            &lt;tr class="DataGridCellData"&gt;
                  &lt;td class="DataGridCellDataCenterAligned"&gt;
                      &lt;span class="DataLabel"&gt;&lt;input id="ctl01_PageBody_grdDocument_ctl316_chkSelect" name="ctl01$PageBody$grdDocument$ctl316$chkSelect" onclick="javascript:uiHelper.GridCellCheck(this, 'chkAllSelect');" type="checkbox"&gt;&lt;/span&gt;
                  &lt;/td&gt;&lt;td&gt;
                      &lt;span id="ctl01_PageBody_grdDocument_ctl316_lblDocumentCategoryDescription"&gt;Approval Form&lt;/span&gt;
                  &lt;/td&gt;&lt;td align="left"&gt;
                      &lt;span id="ctl01_PageBody_grdDocument_ctl316_lblDocumentdescription"&gt;Sublease Addendum&lt;/span&gt;
                  &lt;/td&gt;&lt;td align="center"&gt;
                  &lt;/td&gt;
              &lt;/tr&gt;
  </code></pre>
  
  <p>Thanks in advance for any possible help.</p>