我有一个包含一些行和列的表的页面。第一列有一个复选框(索引为0)。第二列有名字。 表中有一些数据行。我想点击名为“Selenium_CRM_For_Edit_Test”的复选框
我的XPATH不会点击该复选框。我可以使用Firebug中的XPATH检查器找到该复选框。我的XPATH是:
//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
HTML是:
<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>
我的Selenium Python代码选择复选框是:
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
我也尝试在表中循环,从col 1中找到名称,然后单击col 0,这是复选框所在的位置。这是行不通的。我的代码段是:
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
如何点击我想要的复选框?
谢谢, 里亚兹
答案 0 :(得分:2)
你走了:
//tr[td[contains(., "Selenium_CRM_Edit_Test")]]//input
<强>说明:强>
首先,我们查看tr
,然后我们查看td
,其中包含之前查看tr
的特定文字,之后我们会非常轻松地找到所需的复选框。
或强>
你可以先用文字查看跨度,然后按照每个级别向上和向上进行操作:
//span[@title="Selenium_CRM_Edit_Test"]/ancestor::tr//input