Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/selenium/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 2.7 Selenium Webdriver Python如何选择特定复选框_Python 2.7_Selenium_Selenium Webdriver_Webdriver - Fatal编程技术网

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

Python 2.7 Selenium Webdriver Python如何选择特定复选框,python-2.7,selenium,selenium-webdriver,webdriver,Python 2.7,Selenium,Selenium Webdriver,Webdriver,我有一个包含一些行和列的html表。每行在第一列中都有一个复选框。姓名在第二列。 我想选中名为test2的复选框,我将以test2为例 我尝试使用for循环遍历表和行,如果找到名称test2,则单击复选框。 我的代码没有单击复选框 我的代码片段是: from selenium.webdriver.common.by import By from selenium.webdriver.support.ui import WebDriverWait def select_datamap(self,

我有一个包含一些行和列的html表。每行在第一列中都有一个复选框。姓名在第二列。 我想选中名为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>
如何找到与我传递的参数匹配的复选框,然后单击它? 使用for循环会很好,因为参数test2可以位于任何行中,具体取决于客户端在应用程序中输入的名称


谢谢,

实际上,您可以使用以下xpath直接查找复选框,而无需迭代:

//span [text()='test2']/../../preceding-sibling::td/div/input[@type='checkbox']
xpath表达式仍然不是很好,但应该可以工作:

然后做一个。点击结果

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