【问题标题】:Selenium Python my Xpath won't click the checkbox inside the table 1st columnSelenium Python 我的 Xpath 不会单击表第一列中的复选框
【发布时间】:2016-02-11 15:08:05
【问题描述】:

我有一个包含一些行和列的表格的页面。第一列有一个复选框(索引为 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,即复选框所在的位置。这没用。我的代码 sn-p 是:

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

如何点击我想要的复选框?

谢谢, 里亚兹

【问题讨论】:

    标签: python-2.7 selenium xpath selenium-webdriver


    【解决方案1】:

    给你:

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

    解释: 首先我们查看tr,然后我们查看td 以及之前查看tr 中的特定文本,然后我们很容易找到所需的复选框。

    您可以从后面先查看带有文本的跨度,然后像这样逐级上升:

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

    【讨论】:

    • 我也试过 //span[@title = 'Selenium_CRM_Edit_Test']/ancestor::tr[1]//input[@type = 'checkbox'] 这也有效
    • @RiazLadhani 确实如此——这样你的道路就会更加具体。很高兴为您提供帮助!
    猜你喜欢
    • 2016-04-12
    • 1970-01-01
    • 2017-09-24
    • 2021-12-11
    • 1970-01-01
    • 1970-01-01
    • 2013-06-08
    • 1970-01-01
    • 2019-10-10
    相关资源
    最近更新 更多