2016-02-11 18 views
1

我有一個包含一些行和列的表的頁面。第一列有一個複選框(索引爲0)。第二列有名字。 表中有一些數據行。我想單擊名稱爲「Selenium_CRM_For_Edit_Test」的複選框Selenium Python我的Xpath不會點擊表格第1列中的複選框

我的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> 

我的硒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 

我也試圖通過表循環,找到從列1中選擇名稱,然後單擊複選框所在的列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 

我怎樣才能點擊我想要的複選框?

感謝, 里亞茲

回答

2

在這裏你去:

//tr[td[contains(., "Selenium_CRM_Edit_Test")]]//input 

說明: 首先我們看一下tr,那麼我們來看看td與在前面看着特定文本tr之後,我們很容易找到所需的複選框。

OR

您可以從後面走,首先用文字看跨度,然後通過每個級別,像這樣不斷上升,最高:

//span[@title="Selenium_CRM_Edit_Test"]/ancestor::tr//input 
+0

這樣的作品,謝謝。 –

+0

我也試過// span [@title ='Selenium_CRM_Edit_Test']/ancestor :: tr [1] //輸入[@type ='checkbox'],這也適用於 –

+0

@RiazLadhani它的確如此 - 混凝土在你的道路上。樂意效勞! – Newcomer

相關問題