Selenium Webdriver Python如何选择特定的复选框

时间:2015-09-16 10:27:45

标签: python-2.7 selenium selenium-webdriver webdriver

我有一个包含一些行和列的html表。每行在第1列中都有一个复选框。第2栏中的名称。 我想选择名为test2的复选框(我将使用test2作为示例) 我已经尝试使用for循环遍历表和行,如果找到名称test2,则单击复选框。 我的代码没有点击复选框。

我的代码段是:

from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait

def select_datamap(self, datamap_name): 
        try:
            WebDriverWait(self.driver, 20).until(EC.presence_of_all_elements_located((By.TAG_NAME, 'td')))
            table_id = self.driver.find_element(By.ID, 'data_configuration_datamaps_ct_fields_body')
            rows = table_id.find_elements(By.TAG_NAME, "tr")
            for row in rows:
                print row
                # Get the columns
                col_name = row.find_elements(By.TAG_NAME, "td")[1]  # This is the Name column
                print col_name.text
                if (col_name.text == datamap_name):
                    checkbox = self.driver.find_element(By.XPATH, '//table[@id="data_configuration_datamaps_ct_fields_body"]/tbody/tr[%s]/td[1]/div/input') % row
                    #checkbox = self.driver.find_element_by_css_selector("#data_configuration_datamaps_ct_fields_body input[type='checkbox']")
                    checkbox.click()
                    return True
            return False
        except NoSuchElementException, e:
            return False

HTML是:

<table id="data_configuration_datamaps_ct_fields_body" cellspacing="0" style="table-layout: fixed; width: 100%;">
<colgroup>
<tbody>
    <tr class="GOFU2OVFG GOFU2OVMG" __gwt_subrow="0" __gwt_row="0">
        <td class="GOFU2OVEG GOFU2OVGG GOFU2OVHG GOFU2OVNG">
        <td class="GOFU2OVEG GOFU2OVGG GOFU2OVNG">
            <div __gwt_cell="cell-gwt-uid-318" style="outline-style:none;">
                <span class="linkhover" title="DM" style="white-space:nowrap;overflow:hidden;text-overflow:ellipsis;empty-cells:show;display:block;color:#00A;cursor:pointer;">DM</span>
            </div>
        </td>
        <td class="GOFU2OVEG GOFU2OVGG GOFU2OVNG">
        <td class="GOFU2OVEG GOFU2OVGG GOFU2OVBH GOFU2OVNG">
    </tr>
    <tr class="GOFU2OVEH" __gwt_subrow="0" __gwt_row="1">
        <td class="GOFU2OVEG GOFU2OVFH GOFU2OVHG">
            <div __gwt_cell="cell-gwt-uid-317" style="outline-style:none;">
                <input type="checkbox" tabindex="-1"/>
            </div>
        </td>
        <td class="GOFU2OVEG GOFU2OVFH">
            <div __gwt_cell="cell-gwt-uid-318" style="outline-style:none;">
                <span class="linkhover" title="test1" style="white-space:nowrap;overflow:hidden;text-overflow:ellipsis;empty-cells:show;display:block;color:#00A;cursor:pointer;">test1</span>
            </div>
        </td>
        <td class="GOFU2OVEG GOFU2OVFH">
        <td class="GOFU2OVEG GOFU2OVFH GOFU2OVBH">
    </tr>
    <tr class="GOFU2OVFG" __gwt_subrow="0" __gwt_row="2">
        <td class="GOFU2OVEG GOFU2OVGG GOFU2OVHG">
            <div __gwt_cell="cell-gwt-uid-317" style="outline-style:none;">
                <input type="checkbox" tabindex="-1"/>
            </div>
        </td>
        <td class="GOFU2OVEG GOFU2OVGG">
            <div __gwt_cell="cell-gwt-uid-318" style="outline-style:none;">
                <span class="linkhover" title="test2" style="white-space:nowrap;overflow:hidden;text-overflow:ellipsis;empty-cells:show;display:block;color:#00A;cursor:pointer;">test2</span>
            </div>
        </td>
        <td class="GOFU2OVEG GOFU2OVGG">
        <td class="GOFU2OVEG GOFU2OVGG GOFU2OVBH">
    </tr>
</tbody>
</table>
从我的TestCase类

我用参数“test1”

调用select_datamap方法
import unittest
some more imports ...

class DatamapsPage_TestCase(BaseTestCase):

    def test_delete_datamaps(self):
        print "*** Test Delete DM Datamaps ***"
        some steps ...
        ...
        tool_bar = ToolbarPage(self.driver)
        dm = tool_bar.clickMoreTools("Data Configuration", "Datamaps" ) 
        dm.select_datamap("test1")

如何找到与我通过的参数匹配的复选框,然后单击它? 使用for循环会很好,因为参数test2可以在任何行中,具体取决于客户端在应用程序中输入的名称。

谢谢,

1 个答案:

答案 0 :(得分:0)

实际上你可以使用以下xpath直接找到复选框而不需要迭代:

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

仍然是xpath-expression不是很好但是应该可以工作:)

然后只对结果

进行.click()