Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/python-2.7/5.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 我有一个列中带有复选框的行表。如何选择特定的复选框_Python 2.7_Selenium_Xpath_Selenium Webdriver - Fatal编程技术网

Python 2.7 我有一个列中带有复选框的行表。如何选择特定的复选框

Python 2.7 我有一个列中带有复选框的行表。如何选择特定的复选框,python-2.7,selenium,xpath,selenium-webdriver,Python 2.7,Selenium,Xpath,Selenium Webdriver,我有下面的HTML结构。有行的表格,第2列显示文本,例如标题(第1行)、FName(第2行)、SNAME(第3行)、性别等。 第三列的每一行都有一个复选框。 我正在尝试选择一个特定的复选框。例如,我的方法将接受一个参数(行中文本值的名称,例如TITLE)。 该方法将选中标题的复选框。 当我使用参数FNAME再次调用该方法时,将单击FNAME的复选框。 我将SeleniumWebDriver与Python一起使用 我尝试了以下XPATH来标识复选框: //span[@title="TITLE" a

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

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

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

//span [text()="TITLE"]/../../preceding-sibling::td/div/input[@type="checkbox"]
它们找不到名为TITLE的行的复选框

我可以使用以下XPATH获得标题

//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>

标题
FNAME
更多带有复选框等名称的行。。。。。。
我可以使用什么XPATH来获取标题、FNAME等的复选框? 我有表ID“数据\配置\编辑\映射\选项卡\映射\ ct\映射\正文” 也许有一种方法可以从表ID开始,使用for循环遍历行并找到特定的复选框

谢谢


Riaz

您可以使用以下xpath表达式

String xpath = "//span[@title = 'TITLE']/ancestor::tr[1]//input[@type = 'checkbox']"
它的作用是:

  • 首先使用您的参数搜索跨度元素(请将“标题”更改为您正在使用的变量)
  • 然后找到作为tr元素的第一个祖先元素
  • 从那里找到这个tr元素中作为复选框的输入元素
然后你可以提炼成这样的东西:

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

为了遇到类似问题的其他人的利益。我用于上述答案的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也可以使用