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

时间:2015-11-03 12:13:47

标签: css selenium xpath selenium-webdriver

我正在尝试从HTML表格列中选择一个复选框。我想选中包含文字“USN”的复选框 我的XPATH正在选择2个复选框:

  • USN
  • USN日期

我想选中1复选框:

  • USN

我的XPATH是:

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

如何构建我的XPATH或CSS以选择我想要的确切复选框? 而且如果结构改变了XPATH或CSS是健壮的。

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="GLKP2TGBCE" __gwt_subrow="0" __gwt_row="1">
                <td class="GLKP2TGBBD GLKP2TGBDE GLKP2TGBED">
                    <div __gwt_cell="cell-gwt-uid-500" style="outline-style:none;">
                        <input type="checkbox" tabindex="-1" />
                    </div>
                </td>
                <td class="GLKP2TGBBD GLKP2TGBDE">
                    <div __gwt_cell="cell-gwt-uid-501" style="outline-style:none;">DATASET</div>
                </td>
                <td class="GLKP2TGBBD GLKP2TGBDE GLKP2TGBOD">
                    <div __gwt_cell="cell-gwt-uid-502" style="outline-style:none;" />
                </td>
            </tr>
            <tr class="GLKP2TGBCD" __gwt_subrow="0" __gwt_row="2">
                <td class="GLKP2TGBBD GLKP2TGBDD GLKP2TGBED">
                    <div __gwt_cell="cell-gwt-uid-500" style="outline-style:none;">
                        <input type="checkbox" tabindex="-1" />
                    </div>
                </td>
                <td class="GLKP2TGBBD GLKP2TGBDD">
                    <div __gwt_cell="cell-gwt-uid-501" style="outline-style:none;">ID</div>
                </td>
                <td class="GLKP2TGBBD GLKP2TGBDD GLKP2TGBOD">
                    <div __gwt_cell="cell-gwt-uid-502" style="outline-style:none;" />
                </td>
            </tr>
            <tr class="GLKP2TGBCE" __gwt_subrow="0" __gwt_row="3">
                <td class="GLKP2TGBBD GLKP2TGBDE GLKP2TGBED">
                    <div __gwt_cell="cell-gwt-uid-500" style="outline-style:none;">
                        <input type="checkbox" tabindex="-1" />
                    </div>
                </td>
                <td class="GLKP2TGBBD GLKP2TGBDE">
                    <div __gwt_cell="cell-gwt-uid-501" style="outline-style:none;">MATCH_AUDIT</div>
                </td>
                <td class="GLKP2TGBBD GLKP2TGBDE GLKP2TGBOD">
                    <div __gwt_cell="cell-gwt-uid-502" style="outline-style:none;" />
                </td>
            </tr>
            <tr class="GLKP2TGBCD" __gwt_subrow="0" __gwt_row="4">
                <td class="GLKP2TGBBD GLKP2TGBDD GLKP2TGBED">
                    <div __gwt_cell="cell-gwt-uid-500" style="outline-style:none;">
                        <input type="checkbox" tabindex="-1" />
                    </div>
                </td>
                <td class="GLKP2TGBBD GLKP2TGBDD">
                    <div __gwt_cell="cell-gwt-uid-501" style="outline-style:none;">EXPANDED_MATCH_AUDIT</div>
                </td>
                <td class="GLKP2TGBBD GLKP2TGBDD GLKP2TGBOD">
                    <div __gwt_cell="cell-gwt-uid-502" style="outline-style:none;" />
                </td>
            </tr>
            <tr class="GLKP2TGBCE" __gwt_subrow="0" __gwt_row="5">
                <td class="GLKP2TGBBD GLKP2TGBDE GLKP2TGBED">
                    <div __gwt_cell="cell-gwt-uid-500" style="outline-style:none;">
                        <input type="checkbox" tabindex="-1" />
                    </div>
                </td>
                <td class="GLKP2TGBBD GLKP2TGBDE">
                    <div __gwt_cell="cell-gwt-uid-501" style="outline-style:none;">INTERNAL_ID</div>
                </td>
                <td class="GLKP2TGBBD GLKP2TGBDE GLKP2TGBOD">
                    <div __gwt_cell="cell-gwt-uid-502" style="outline-style:none;" />
                </td>
            </tr>
            <tr class="GLKP2TGBCD" __gwt_subrow="0" __gwt_row="6">
                <td class="GLKP2TGBBD GLKP2TGBDD GLKP2TGBED">
                    <div __gwt_cell="cell-gwt-uid-500" style="outline-style:none;">
                        <input type="checkbox" tabindex="-1" />
                    </div>
                </td>
                <td class="GLKP2TGBBD GLKP2TGBDD">
                    <div __gwt_cell="cell-gwt-uid-501" style="outline-style:none;">USN_DATE</div>
                </td>
                <td class="GLKP2TGBBD GLKP2TGBDD GLKP2TGBOD">
                    <div __gwt_cell="cell-gwt-uid-502" style="outline-style:none;" />
                </td>
            </tr>
            <tr class="GLKP2TGBCE" __gwt_subrow="0" __gwt_row="7">
                <td class="GLKP2TGBBD GLKP2TGBDE GLKP2TGBED">
                    <div __gwt_cell="cell-gwt-uid-500" style="outline-style:none;">
                        <input type="checkbox" tabindex="-1" />
                    </div>
                </td>
                <td class="GLKP2TGBBD GLKP2TGBDE">
                    <div __gwt_cell="cell-gwt-uid-501" style="outline-style:none;">VERSION</div>
                </td>
                <td class="GLKP2TGBBD GLKP2TGBDE GLKP2TGBOD">
                    <div __gwt_cell="cell-gwt-uid-502" style="outline-style:none;" />
                </td>
            </tr>
            <tr class="GLKP2TGBCD" __gwt_subrow="0" __gwt_row="8">
                <tr class="GLKP2TGBCE" __gwt_subrow="0" __gwt_row="9">
                    <tr class="GLKP2TGBCD" __gwt_subrow="0" __gwt_row="10">
        </tbody>
        <tbody style="display: none;">
            <tfoot style="display: none;" aria-hidden="true" />

谢谢, 里亚兹

1 个答案:

答案 0 :(得分:2)

//*[text() = 'USN']

此xpath可以使用,但这个是一个很长的查询..尝试使用text()= 'USN'这个与你的一起而不是包含。