Selenium XPATH或CSS从html表中选择一个带有指定文本的复选框我选中了四个复选框

时间:2015-11-03 14:04:21

标签: css selenium xpath selenium-webdriver

我正在尝试从HTML表格列中选择一个复选框。我想选择名称为“CLEAN_AUDIT”的复选框 我的XPATH为名称,DOB,地址,电话选择了4个复选框CLEAN_AUDIT:

表格在页面上的显示方式示例如下:

Table Header:             field        variable
               checkbox | Time_STAMP  | DOB
               checkbox | Time_STAMP  | Address
               checkbox | Clean_AUDIT | Name
               checkbox | Clean_AUDIT | Address
               checkbox | Clean_AUDIT | DOB
               checkbox | Clean_AUDIT | Phone
               etc...

我想选中1复选框:

  • Clean_AUDIT for Name

我的XPATH正在选择4个复选框:

  • Clean_AUDIT for Name
  • Clean_AUDIT for Address
  • DOB的Clean_AUDIT
  • Clean_AUDIT for Phone

表中的第三列是变量列。它在这一个中有名字,我需要第二列的字段,即Field列。

如何构建XPATH以查找具有“Name”的第3列和具有“Clean_AUDIT”的第2列

在我的下一个测试中,我可以找到“地址”,其中包含“Clean_AUDIT”

我的XPATH找到所有变量的所有Clean_AUDIT:

//table[@id="reporting_add_report_tab_manual_ct_fields_avail"]//div[(text()="CLEAN_AUDIT")]/preceding::td[1]//input

我需要以某种方式包含姓名,以便找到我想要的正确的Clean_Audit。

示例HTML是(我缩短了它,否则粘贴在这里太长了:

<table id="reporting_add_report_tab_manual_ct_fields_avail" class="GLKP2TGBJE" cellspacing="0" __gwtcellbasedwidgetimpldispatchingfocus="true" __gwtcellbasedwidgetimpldispatchingblur="true">
<thead aria-hidden="false">
    <colgroup>
        <tbody style="">
            <tr class="GLKP2TGBCD GLKP2TGBJD" __gwt_subrow="0" __gwt_row="0">
                <td class="GLKP2TGBBD GLKP2TGBDD GLKP2TGBED GLKP2TGBKD">
                    <div __gwt_cell="cell-gwt-uid-500" style="outline-style:none;">
                        <input type="checkbox" tabindex="-1" />
                    </div>
                </td>
                <td class="GLKP2TGBBD GLKP2TGBDD GLKP2TGBKD">
                    <div __gwt_cell="cell-gwt-uid-501" style="outline-style:none;">USN</div>
                </td>
                <td class="GLKP2TGBBD GLKP2TGBDD GLKP2TGBOD GLKP2TGBKD">
                    <div __gwt_cell="cell-gwt-uid-502" style="outline-style:none;" />
                </td>
            </tr>
            <tr class="GLKP2TGBCD" __gwt_subrow="0" __gwt_row="52">
                <td class="GLKP2TGBBD GLKP2TGBDD GLKP2TGBED">
                    <div __gwt_cell="cell-gwt-uid-552" style="outline-style:none;">
                        <input type="checkbox" tabindex="-1" />
                    </div>
                </td>
                <td class="GLKP2TGBBD GLKP2TGBDD">
                    <div __gwt_cell="cell-gwt-uid-553" style="outline-style:none;">CLEAN_AUDIT</div>
                </td>
                <td class="GLKP2TGBBD GLKP2TGBDD GLKP2TGBOD">
                    <div __gwt_cell="cell-gwt-uid-554" style="outline-style:none;">Address</div>
                </td>
            </tr>
            <tr class="GLKP2TGBCD" __gwt_subrow="0" __gwt_row="20">
                <td class="GLKP2TGBBD GLKP2TGBDD GLKP2TGBED">
                    <div __gwt_cell="cell-gwt-uid-552" style="outline-style:none;">
                        <input type="checkbox" tabindex="-1" />
                    </div>
                </td>
                <td class="GLKP2TGBBD GLKP2TGBDD">
                    <div __gwt_cell="cell-gwt-uid-553" style="outline-style:none;">EXPANDED_CLEAN_AUDIT</div>
                </td>
                <td class="GLKP2TGBBD GLKP2TGBDD GLKP2TGBOD">
                    <div __gwt_cell="cell-gwt-uid-554" style="outline-style:none;">Name</div>
                </td>
            </tr>
            <tr class="GLKP2TGBCE" __gwt_subrow="0" __gwt_row="21">
                <td class="GLKP2TGBBD GLKP2TGBDE GLKP2TGBED">
                    <div __gwt_cell="cell-gwt-uid-552" style="outline-style:none;">
                        <input type="checkbox" tabindex="-1" />
                    </div>
                </td>
                <td class="GLKP2TGBBD GLKP2TGBDE">
                    <div __gwt_cell="cell-gwt-uid-553" style="outline-style:none;">CATEGORY</div>
                </td>
                <td class="GLKP2TGBBD GLKP2TGBDE GLKP2TGBOD">
                    <div __gwt_cell="cell-gwt-uid-554" style="outline-style:none;">Name</div>
                </td>
            </tr>
            <tr class="GLKP2TGBCD" __gwt_subrow="0" __gwt_row="62">
                <td class="GLKP2TGBBD GLKP2TGBDD GLKP2TGBED">
                    <div __gwt_cell="cell-gwt-uid-552" style="outline-style:none;">
                        <input type="checkbox" tabindex="-1" />
                    </div>
                </td>
                <td class="GLKP2TGBBD GLKP2TGBDD">
                    <div __gwt_cell="cell-gwt-uid-553" style="outline-style:none;">CLEAN_AUDIT</div>
                </td>
                <td class="GLKP2TGBBD GLKP2TGBDD GLKP2TGBOD">
                    <div __gwt_cell="cell-gwt-uid-554" style="outline-style:none;">DOB</div>
                </td>
            </tr>
        </tbody>
        <tbody style="display: none;">
            <tfoot style="display: none;" aria-hidden="true" />

谢谢, 里亚兹

1 个答案:

答案 0 :(得分:0)

已更新

WebElement test = getDriver().findElement(By.xpath("//table[@id='reporting_add_report_tab_manual_ct_fields_avail']//div[(text()='CLEAN_AUDIT')]/following::div[(text()='Name')]/preceding::td[1]/preceding::td[1]//input"));
test.click();

正在运作示例。我已经使用原始问题中的示例测试了此片段。