Reputation: 4072
I have a page with a table with some rows and columns. The first column has a checkbox (index is 0). The 2nd column has the name. There are some rows of data in the table. I would like to click the checkbox which has the name "Selenium_CRM_For_Edit_Test"
My XPATH won't click the checkbox. I can locate the checkbox using XPATH checker in Firebug. My XPATH is:
//table[@id="data_configuration_data_previews_ct_fields_body"]/tbody/tr//../td//../div/span[contains(text(), "Selenium_CRM_For_Edit_Test")]/preceding::div[1]//../input
The HTML is:
<table id="data_configuration_data_previews_ct_fields_body" cellspacing="0" style="table-layout: fixed; width: 100%;">
<colgroup>
<tbody>
<tr class="GJPPK2LBFG" __gwt_subrow="0" __gwt_row="0">
<tr>.. some more rows here
<tr class="GJPPK2LBEH" __gwt_subrow="0" __gwt_row="15">
<tr class="GJPPK2LBFG" __gwt_subrow="0" __gwt_row="16">
<td class="GJPPK2LBEG GJPPK2LBGG GJPPK2LBHG">
<div __gwt_cell="cell-gwt-uid-1417" style="outline-style:none;">
<input type="checkbox" tabindex="-1"/>
</div>
</td>
<td class="GJPPK2LBEG GJPPK2LBGG">
<div __gwt_cell="cell-gwt-uid-1418" style="outline-style:none;">
<span class="linkhover" title="MegaOne_CHROME" style="white-space:nowrap;overflow:hidden;text-overflow:ellipsis;empty-cells:show;display:block;color:#00A;cursor:pointer;">MegaOne_CHROME</span>
</div>
</td>
<td class="GJPPK2LBEG GJPPK2LBGG">
<td class="GJPPK2LBEG GJPPK2LBGG">
<td class="GJPPK2LBEG GJPPK2LBGG GJPPK2LBBH">
</tr>
<tr class="GJPPK2LBEH GJPPK2LBMG" __gwt_subrow="0" __gwt_row="17">
<td class="GJPPK2LBEG GJPPK2LBFH GJPPK2LBHG GJPPK2LBNG">
<div __gwt_cell="cell-gwt-uid-1417" style="outline-style:none;">
<input type="checkbox" tabindex="-1"/>
</div>
</td>
<td class="GJPPK2LBEG GJPPK2LBFH GJPPK2LBNG">
<div __gwt_cell="cell-gwt-uid-1418" style="outline-style:none;">
<span class="linkhover" title="Selenium_CRM_Edit_Test" style="white-space:nowrap;overflow:hidden;text-overflow:ellipsis;empty-cells:show;display:block;color:#00A;cursor:pointer;">Selenium_CRM_Edit_Test</span>
</div>
</td>
<td class="GJPPK2LBEG GJPPK2LBFH GJPPK2LBNG">
<div __gwt_cell="cell-gwt-uid-1419" style="outline-style:none;">
<span class="linkhover" title="view" style="white-space:nowrap;overflow:hidden;text-overflow:ellipsis;empty-cells:show;display:block;color:#00A;cursor:pointer;">view</span>
</div>
</td>
<td class="GJPPK2LBEG GJPPK2LBFH GJPPK2LBNG">
<div __gwt_cell="cell-gwt-uid-1420" style="outline-style:none;">File</div>
</td>
<td class="GJPPK2LBEG GJPPK2LBFH GJPPK2LBBH GJPPK2LBNG">
<div __gwt_cell="cell-gwt-uid-1421" style="outline-style:none;">498</div>
</td>
</tr>
<tr class="GJPPK2LBFG" __gwt_subrow="0" __gwt_row="18">
<tr class="GJPPK2LBEH" __gwt_subrow="0" __gwt_row="19">
<tr class="GJPPK2LBFG" __gwt_subrow="0" __gwt_row="20">
</tbody>
</table>
My Selenium Python code to select the checkbox is:
def select_a_data_preview(self, data_preview):
# to get to the checkbox By.XPATH //table[@id="data_configuration_data_previews_ct_fields_body"]/tbody/tr//../td//../div/span[contains(text(), "Selenium_LADEMO_CRM_DONOTCHANGE")]/preceding::div[1]//../input
data_preview_element = self.driver.find_element(By.XPATH, '//table[@id="data_configuration_data_previews_ct_fields_body"]/tbody/tr//../td//../div/span[contains(text(), "%s")]/preceding::div[1]//../input' % data_preview)
data_preview_element.click()
return self
I have also tried looping through the table, find the name from col 1 and then click col 0 which is where the checkbox is. It does not work. My code snippet is:
def select_a_data_preview(self, data_preview):
try:
WebDriverWait(self.driver, 20).until(EC.presence_of_element_located((By.ID, 'data_configuration_data_previews_ct_fields_body')))
table_id = WebDriverWait(self.driver, 20).until(EC.presence_of_element_located((By.ID, 'data_configuration_data_previews_ct_fields_body')))
rows = table_id.find_elements(By.TAG_NAME, "tr")
for row in rows:
# Get the columns
col_checkbox = row.find_elements(By.TAG_NAME, "td")[0] # This ist the checkbox col
col_name = row.find_elements(By.TAG_NAME, "td")[1] # This is the Configuration Name column
col_type = row.find_elements(By.TAG_NAME, "td")[3] # This is the Type column
col_rows = row.find_elements(By.TAG_NAME, "td")[4] # This is the Rows column
print "col_name.text = "
print col_name.text
print col_type.text
print col_rows.text
if (col_name.text == data_preview):
col_checkbox.click()
return True
return False
except NoSuchElementException, e:
print "Element not found "
print e
self.save_screenshot("data_previews_page_select_element")
return False
return self
How can i click the checkbox that i want?
Thanks, Riaz
Upvotes: 1
Views: 827
Reputation: 503
Here you go:
//tr[td[contains(., "Selenium_CRM_Edit_Test")]]//input
Explanation:
First we look at the tr
, then we look at the td
with specific text that's in the previously looked at tr
and after that we find the needed checkbox very easily.
OR
You can go from behind by looking at the span with text first and then going up and up by each level like so:
//span[@title="Selenium_CRM_Edit_Test"]/ancestor::tr//input
Upvotes: 2