我有一个行表,列中有复选框。如何选择特定复选框

时间:2015-09-18 08:59:51

标签: python-2.7 selenium xpath selenium-webdriver

我有以下HTML结构。具有行的表,第二列具有显示的文本,例如标题(第1行),FName(第2行),SNAME(第3行),GENDER等 第3列每行都有一个复选框。 我正在尝试选择一个特定的复选框。例如。我的方法将接受一个参数(行中文本值的名称,例如TITLE)。 该方法将选中TITLE的复选框。 当我使用参数FNAME再次调用该方法时,将单击FNAME的复选框。 我正在使用Selenium Webdriver和Python

我尝试了以下XPATH来识别复选框:

//span[@title="TITLE" and contains(text(), "TITLE")]/following-sibling::*

//span [text()="TITLE"]/../../preceding-sibling::td/div/input[@type="checkbox"]

这些找不到名为TITLE

的行的复选框

我可以通过以下XPATH访问TITLE。

//span [text()="TITLE"]

我的代码段是:

        def add_mapping2(self, name):
    try:
        checkbox = self.driver.find_element(By.XPATH, '//span [text()="+name+"]/../../preceding-sibling::td/div/input[@type="checkbox"]') 
        checkbox.click()
    except NoSuchElementException, e:
        return False
    return True

从我的unittest.Testcase类中,我按如下方式调用该方法:

class MappingsPage_TestCase(BaseTestCase):

    def test_add_mappings(self):
        mappingsPage = projectNavigator.select_projectNavigator_item("Mappings")
        mappingsPage.add_mapping2("TITLE")
        mappingsPage.add_mapping2("SNAME")

HTML是:

 <table id="data_configuration_edit_mapping_tab_mappings_ct_mapping_body" cellspacing="0" style="table-layout: fixed; width: 100%;">
<colgroup>
<tbody>
    <tr class="GOFU2OVFG" __gwt_subrow="0" __gwt_row="0">
    <tr class="GOFU2OVEH GOFU2OVGH GOFU2OVPG GOFU2OVMG" __gwt_subrow="0" __gwt_row="1">
        <td class="GOFU2OVEG GOFU2OVFH GOFU2OVHG GOFU2OVHH GOFU2OVAH GOFU2OVNG">
        <td class="GOFU2OVEG GOFU2OVFH GOFU2OVHH GOFU2OVAH GOFU2OVNG">
            <div __gwt_cell="cell-gwt-uid-792" style="outline-style:none;">
                <span title="TITLE" style="white-space:nowrap;overflow:hidden;text-overflow:ellipsis;empty-cells:show;display:block;padding-right: 1px;">TITLE</span>
            </div>
        </td>
        <td class="GOFU2OVEG GOFU2OVFH GOFU2OVHH GOFU2OVBH GOFU2OVOG GOFU2OVAH GOFU2OVNG">
            <div __gwt_cell="cell-gwt-uid-793" style="outline-style:none;" tabindex="0">
                <input type="checkbox" checked="" tabindex="-1"/>
            </div>
        </td>
    </tr>
    <tr class="GOFU2OVFG" __gwt_subrow="0" __gwt_row="2">
        <td class="GOFU2OVEG GOFU2OVGG GOFU2OVHG">
            <div __gwt_cell="cell-gwt-uid-791" style="outline-style:none;">
                <input type="radio" name="rbCrossRow124"/>
            </div>
        </td>
        <td class="GOFU2OVEG GOFU2OVGG">
            <div __gwt_cell="cell-gwt-uid-792" style="outline-style:none;">
                <span class="" title="FNAME" style="white-space:nowrap;overflow:hidden;text-overflow:ellipsis;empty-cells:show;display:block;padding-right: 1px;">FNAME</span>
            </div>
        </td>
        <td class="GOFU2OVEG GOFU2OVGG GOFU2OVBH">
            <div __gwt_cell="cell-gwt-uid-793" style="outline-style:none;">
                <input type="checkbox" tabindex="-1"/>
            </div>
        </td>
    </tr>
    <tr class="GOFU2OVEH" __gwt_subrow="0" __gwt_row="3">
    <tr class="GOFU2OVFG" __gwt_subrow="0" __gwt_row="4">
    <tr class="GOFU2OVEH" __gwt_subrow="0" __gwt_row="5">
    <tr class="GOFU2OVFG" __gwt_subrow="0" __gwt_row="6">
    more rows with names with checkboxes etc......
</tbody>
</table>

我可以用什么XPATH来获取TITLE,FNAME等的复选框? 我有表ID&#34; data_configuration_edit_mapping_tab_mappings_ct_mapping_body&#34; 也许有一种方法可以从表ID开始并使用for循环遍历行并找到特定的复选框?

感谢。

里亚兹

2 个答案:

答案 0 :(得分:2)

您将使用以下xpath表达式

String xpath = "//span[@title = 'TITLE']/ancestor::tr[1]//input[@type = 'checkbox']"

它的作用:

  • 首先使用您的参数搜索span元素(请将'TITLE'更改为您正在使用的变量)
  • 然后找到第一个作为tr元素的祖先元素
  • 从那里找到输入元素,这是你在这个tr元素中的复选框

然后你可以像这样改进:

WebElement table = driver.findElement(By.id("data_configuration_edit_mapping_tab_mappings_ct_mapping_body"));
WebElement checkbox = table.findElement(By.xpath(xpath));

答案 1 :(得分:0)

为了遇到类似问题的其他人的利益。我用于上述答案的Python代码是:

from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait

def add_mapping(self, name):
        wait = WebDriverWait(self.driver, 10)
        try:
            checkbox = wait.until(EC.element_to_be_clickable((By.XPATH, '//span[@title = "%s"]/ancestor::tr[1]//input[@type = "checkbox"]' % name)))
            #table_id = self.driver.find_element(wait.until(EC.element_to_be_clickable(By.ID, 'data_configuration_edit_mapping_tab_mappings_ct_mapping_body')))
            #checkbox = table_id.find_element(By.XPATH, '//span[@title = "TITLE"]/ancestor::tr[1]//input[@type = "checkbox"]')
            checkbox.click()
        except NoSuchElementException, e:
            return False
        return True

我注释掉的表ID也有效。