【问题标题】:Selenium Webdriver Python how do i select a particular checkboxSelenium Webdriver Python我如何选择一个特定的复选框
【发布时间】:2015-09-16 21:58:06
【问题描述】:

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

我的代码 sn-p 是:

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 可以在任何行中,具体取决于客户端在应用程序中输入的名称。

谢谢,

【问题讨论】:

    标签: python-2.7 selenium selenium-webdriver webdriver


    【解决方案1】:

    其实你可以使用下面的 xpath 直接找到复选框而无需迭代:

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

    xpath 表达式仍然不是很好,但它应该可以工作:)

    然后在结果上创建一个.click()

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-06-09
      • 2017-11-10
      • 1970-01-01
      • 2019-02-06
      • 2017-02-15
      • 2019-12-18
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多