Selenium Python XPATH一个包含行和列的表如何从col1中选择复选框,其中name在col2和col5中

时间:2016-02-24 17:09:20

标签: python-2.7 selenium xpath selenium-webdriver

我有一个包含6列和一些行的表。对于第1行,Col0有一个复选框。第2栏有文字"姓名"和col 5有文本" CRM" 第2行第2行有文字"地址"并有文本" CRM" 第7行第2行有文字"名称"和col 5有文本" ESCR" 这种模式就像这样的日期行。

我想从col0中选中复选框,其中col2具有文本" Name"和col5有文本" CRM"

我如何使用XPATH做到这一点?

我有一个开始,我可以识别文本" CRM"来自col5。我需要得到正确的行,所以我需要找到" Name"然后转到复选框。

我的XPATH是:

//table[@id="data_configuration_variables_ct_fields_body"]/tbody/tr//td/div/span[@title="CRM" and contains(text(), "CRM")]

我想要包含文字"姓名"在XPATH中然后包含复选框。 然后我可以选择正确的行。

HTML示例是(抱歉长HTML)我不知道如何以更短的方式提供它:

    <table id="data_configuration_variables_ct_fields_body" cellspacing="0" style="table-layout: fixed; width: 100%;">
<colgroup>
<tbody>
    <tr class="GJPPK2LBFG" __gwt_subrow="0" __gwt_row="0">
        <td class="GJPPK2LBEG GJPPK2LBGG GJPPK2LBHG">
            <div __gwt_cell="cell-gwt-uid-1130" style="outline-style:none;" tabindex="0">
                <input type="checkbox" tabindex="-1"/>
            </div>
            </td>
            <td class="GJPPK2LBEG GJPPK2LBGG">
                <div __gwt_cell="cell-gwt-uid-1131" style="outline-style:none;">
                    <span class="linkhover" title="Name" style="white-space:nowrap;overflow:hidden;text-overflow:ellipsis;empty-cells:show;display:block;color:#00A;cursor:pointer;">Name</span>
                </div>
            </td>
            <td class="GJPPK2LBEG GJPPK2LBGG">
                <div __gwt_cell="cell-gwt-uid-1132" style="outline-style:none;">
                    <span class="" title="Name" style="white-space:nowrap;overflow:hidden;text-overflow:ellipsis;empty-cells:show;display:block;padding-right: 1px;">Name</span>
                </div>
            </td>
            <td class="GJPPK2LBEG GJPPK2LBGG">
                <div __gwt_cell="cell-gwt-uid-1133" style="outline-style:none;">
                    <span class="" title="" style="white-space:nowrap;overflow:hidden;text-overflow:ellipsis;empty-cells:show;display:block;padding-right: 1px;"/>
                </div>
            </td>
            <td class="GJPPK2LBEG GJPPK2LBGG">
                <div __gwt_cell="cell-gwt-uid-1134" style="outline-style:none;">
                    <span title="CRM" style="white-space:nowrap;overflow:hidden;text-overflow:ellipsis;empty-cells:show;display:block;padding-right: 1px;">CRM</span>
                </div>
            </td>
            <td class="GJPPK2LBEG GJPPK2LBGG GJPPK2LBBH">
            </tr>
            <tr class="GJPPK2LBEH" __gwt_subrow="0" __gwt_row="1">
                <td class="GJPPK2LBEG GJPPK2LBFH GJPPK2LBHG">
                <div __gwt_cell="cell-gwt-uid-1130" style="outline-style:none;">
                    <input type="checkbox" tabindex="-1"/>
                </div>
            </td>
            <td class="GJPPK2LBEG GJPPK2LBFH">
                <div __gwt_cell="cell-gwt-uid-1131" style="outline-style:none;">
                    <span class="linkhover" title="Address" style="white-space:nowrap;overflow:hidden;text-overflow:ellipsis;empty-cells:show;display:block;color:#00A;cursor:pointer;">Address</span>
                </div>
            </td>
            <td class="GJPPK2LBEG GJPPK2LBFH">
                <div __gwt_cell="cell-gwt-uid-1132" style="outline-style:none;">
                    <span class="" title="Address" style="white-space:nowrap;overflow:hidden;text-overflow:ellipsis;empty-cells:show;display:block;padding-right: 1px;">Address</span>
                </div>
            </td>
            <td class="GJPPK2LBEG GJPPK2LBFH">
                <div __gwt_cell="cell-gwt-uid-1133" style="outline-style:none;">
                    <span class="" title="" style="white-space:nowrap;overflow:hidden;text-overflow:ellipsis;empty-cells:show;display:block;padding-right: 1px;"/>
                </div>
            </td>
            <td class="GJPPK2LBEG GJPPK2LBFH">
                <div __gwt_cell="cell-gwt-uid-1134" style="outline-style:none;">
                    <span class="" title="CRM" style="white-space:nowrap;overflow:hidden;text-overflow:ellipsis;empty-cells:show;display:block;padding-right: 1px;">CRM</span>
            </div>
            </td>
            <td class="GJPPK2LBEG GJPPK2LBFH GJPPK2LBBH">
            </tr>
            <tr class="GJPPK2LBFG" __gwt_subrow="0" __gwt_row="2">
            <td class="GJPPK2LBEG GJPPK2LBGG GJPPK2LBHG">
                <div __gwt_cell="cell-gwt-uid-1130" style="outline-style:none;">
                    <input type="checkbox" tabindex="-1"/>
                </div>
            </td>
            <td class="GJPPK2LBEG GJPPK2LBGG">
                <div __gwt_cell="cell-gwt-uid-1131" style="outline-style:none;">
                    <span class="linkhover" title="DOB" style="white-space:nowrap;overflow:hidden;text-overflow:ellipsis;empty-cells:show;display:block;color:#00A;cursor:pointer;">DOB</span>
                </div>
            </td>
            <td class="GJPPK2LBEG GJPPK2LBGG">
                <div __gwt_cell="cell-gwt-uid-1132" style="outline-style:none;">
                    <span class="" title="DOB" style="white-space:nowrap;overflow:hidden;text-overflow:ellipsis;empty-cells:show;display:block;padding-right: 1px;">DOB</span>
                </div>
            </td>
            <td class="GJPPK2LBEG GJPPK2LBGG">
                <div __gwt_cell="cell-gwt-uid-1133" style="outline-style:none;">
                    <span class="" title="" style="white-space:nowrap;overflow:hidden;text-overflow:ellipsis;empty-cells:show;display:block;padding-right: 1px;"/>
                </div>
            </td>
            <td class="GJPPK2LBEG GJPPK2LBGG">
                <div __gwt_cell="cell-gwt-uid-1134" style="outline-style:none;">
                    <span class="" title="CRM" style="white-space:nowrap;overflow:hidden;text-overflow:ellipsis;empty-cells:show;display:block;padding-right: 1px;">CRM</span>
                </div>
            </td>
            <td class="GJPPK2LBEG GJPPK2LBGG GJPPK2LBBH">
            </tr>
            <tr class="GJPPK2LBEH" __gwt_subrow="0" __gwt_row="3">
            <td class="GJPPK2LBEG GJPPK2LBFH GJPPK2LBHG">
                <div __gwt_cell="cell-gwt-uid-1130" style="outline-style:none;">
                    <input type="checkbox" tabindex="-1"/>
                </div>
            </td>
            <td class="GJPPK2LBEG GJPPK2LBFH">
                <div __gwt_cell="cell-gwt-uid-1131" style="outline-style:none;">
                    <span class="linkhover" title="Phone" style="white-space:nowrap;overflow:hidden;text-overflow:ellipsis;empty-cells:show;display:block;color:#00A;cursor:pointer;">Phone</span>
                </div>
            </td>
            <td class="GJPPK2LBEG GJPPK2LBFH">
                <div __gwt_cell="cell-gwt-uid-1132" style="outline-style:none;">
                    <span class="" title="Phone" style="white-space:nowrap;overflow:hidden;text-overflow:ellipsis;empty-cells:show;display:block;padding-right: 1px;">Phone</span>
                </div>
            </td>
            <td class="GJPPK2LBEG GJPPK2LBFH">
                <div __gwt_cell="cell-gwt-uid-1133" style="outline-style:none;">
                    <span class="" title="" style="white-space:nowrap;overflow:hidden;text-overflow:ellipsis;empty-cells:show;display:block;padding-right: 1px;"/>
                </div>
            </td>
            <td class="GJPPK2LBEG GJPPK2LBFH">
            <div __gwt_cell="cell-gwt-uid-1134" style="outline-style:none;">
                <span class="" title="CRM" style="white-space:nowrap;overflow:hidden;text-overflow:ellipsis;empty-cells:show;display:block;padding-right: 1px;">CRM</span>
            </div>
            </td>
            <td class="GJPPK2LBEG GJPPK2LBFH GJPPK2LBBH">
            </tr>
            <tr class="GJPPK2LBFG" __gwt_subrow="0" __gwt_row="4">
            <td class="GJPPK2LBEG GJPPK2LBGG GJPPK2LBHG">
                <div __gwt_cell="cell-gwt-uid-1130" style="outline-style:none;">
                    <input type="checkbox" tabindex="-1"/>
                </div>
            </td>
            <td class="GJPPK2LBEG GJPPK2LBGG">
                <div __gwt_cell="cell-gwt-uid-1131" style="outline-style:none;">
                    <span class="linkhover" title="Email" style="white-space:nowrap;overflow:hidden;text-overflow:ellipsis;empty-cells:show;display:block;color:#00A;cursor:pointer;">Email</span>
                </div>
            </td>
            <td class="GJPPK2LBEG GJPPK2LBGG">
                <div __gwt_cell="cell-gwt-uid-1132" style="outline-style:none;">
                    <span title="Email" style="white-space:nowrap;overflow:hidden;text-overflow:ellipsis;empty-cells:show;display:block;padding-right: 1px;">Email</span>
                </div>
            </td>
            <td class="GJPPK2LBEG GJPPK2LBGG">
                <div __gwt_cell="cell-gwt-uid-1133" style="outline-style:none;">
                    <span class="" title="" style="white-space:nowrap;overflow:hidden;text-overflow:ellipsis;empty-cells:show;display:block;padding-right: 1px;"/>
                </div>
            </td>
            <td class="GJPPK2LBEG GJPPK2LBGG">
                <div __gwt_cell="cell-gwt-uid-1134" style="outline-style:none;">
                    <span class="" title="CRM" style="white-space:nowrap;overflow:hidden;text-overflow:ellipsis;empty-cells:show;display:block;padding-right: 1px;">CRM</span>
            </div>
            </td>
            <td class="GJPPK2LBEG GJPPK2LBGG GJPPK2LBBH">
                <div __gwt_cell="cell-gwt-uid-1135" style="outline-style:none;">
                    <span class="" title="DM" style="white-space:nowrap;overflow:hidden;text-overflow:ellipsis;empty-cells:show;display:block;padding-right: 1px;">DM</span>
                </div>
            </td>
            </tr>
            <tr class="GJPPK2LBEH" __gwt_subrow="0" __gwt_row="5">
            <td class="GJPPK2LBEG GJPPK2LBFH GJPPK2LBHG">
                <div __gwt_cell="cell-gwt-uid-1130" style="outline-style:none;">
                    <input type="checkbox" tabindex="-1"/>
                </div>
            </td>
            <td class="GJPPK2LBEG GJPPK2LBFH">
            <div __gwt_cell="cell-gwt-uid-1131" style="outline-style:none;">
                <span class="linkhover" title="ACVSEQ" style="white-space:nowrap;overflow:hidden;text-overflow:ellipsis;empty-cells:show;display:block;color:#00A;cursor:pointer;">ACVSEQ</span>
            </div>
            </td>
            <td class="GJPPK2LBEG GJPPK2LBFH">
                <div __gwt_cell="cell-gwt-uid-1132" style="outline-style:none;">
                    <span class="" title="ACVSEQ" style="white-space:nowrap;overflow:hidden;text-overflow:ellipsis;empty-cells:show;display:block;padding-right: 1px;">ACVSEQ</span>
                </div>
            </td>
            <td class="GJPPK2LBEG GJPPK2LBFH">
                <div __gwt_cell="cell-gwt-uid-1133" style="outline-style:none;">
                    <span class="" title="" style="white-space:nowrap;overflow:hidden;text-overflow:ellipsis;empty-cells:show;display:block;padding-right: 1px;"/>
                </div>
            </td>
            <td class="GJPPK2LBEG GJPPK2LBFH">
                <div __gwt_cell="cell-gwt-uid-1134" style="outline-style:none;">
                    <span class="" title="CRM" style="white-space:nowrap;overflow:hidden;text-overflow:ellipsis;empty-cells:show;display:block;padding-right: 1px;">CRM</span>
            </div>
            </td>
            <td class="GJPPK2LBEG GJPPK2LBFH GJPPK2LBBH">
                <div __gwt_cell="cell-gwt-uid-1135" style="outline-style:none;">
                    <span class="" title="DM" style="white-space:nowrap;overflow:hidden;text-overflow:ellipsis;empty-cells:show;display:block;padding-right: 1px;">DM</span>
                </div>
            </td>
            </tr>
            <tr class="GJPPK2LBFG" __gwt_subrow="0" __gwt_row="6">
            <td class="GJPPK2LBEG GJPPK2LBGG GJPPK2LBHG">
                <div __gwt_cell="cell-gwt-uid-1130" style="outline-style:none;">
                    <input type="checkbox" tabindex="-1"/>
                </div>
            </td>
            <td class="GJPPK2LBEG GJPPK2LBGG">
                <div __gwt_cell="cell-gwt-uid-1131" style="outline-style:none;">
                    <span class="linkhover" title="Name" style="white-space:nowrap;overflow:hidden;text-overflow:ellipsis;empty-cells:show;display:block;color:#00A;cursor:pointer;">Name</span>
                </div>
            </td>
            <td class="GJPPK2LBEG GJPPK2LBGG">
                <div __gwt_cell="cell-gwt-uid-1132" style="outline-style:none;">
                    <span class="" title="Name" style="white-space:nowrap;overflow:hidden;text-overflow:ellipsis;empty-cells:show;display:block;padding-right: 1px;">Name</span>
                </div>
            </td>
            <td class="GJPPK2LBEG GJPPK2LBGG">
                <div __gwt_cell="cell-gwt-uid-1133" style="outline-style:none;">
                    <span class="" title="" style="white-space:nowrap;overflow:hidden;text-overflow:ellipsis;empty-cells:show;display:block;padding-right: 1px;"/>
                </div>
            </td>
            <td class="GJPPK2LBEG GJPPK2LBGG">
                <div __gwt_cell="cell-gwt-uid-1134" style="outline-style:none;">
                    <span class="" title="ESCR" style="white-space:nowrap;overflow:hidden;text-overflow:ellipsis;empty-cells:show;display:block;padding-right: 1px;">ESCR</span>
                </div>
            </td>
            <td class="GJPPK2LBEG GJPPK2LBGG GJPPK2LBBH">
                <div __gwt_cell="cell-gwt-uid-1135" style="outline-style:none;">
                    <span class="" title="DM" style="white-space:nowrap;overflow:hidden;text-overflow:ellipsis;empty-cells:show;display:block;padding-right: 1px;">DM</span>
                </div>
            </td>
            </tr>
            <tr class="GJPPK2LBEH" __gwt_subrow="0" __gwt_row="7">
            <tr class="GJPPK2LBFG" __gwt_subrow="0" __gwt_row="8">
            <tr class="GJPPK2LBEH" __gwt_subrow="0" __gwt_row="9">
            <tr class="GJPPK2LBFG" __gwt_subrow="0" __gwt_row="10">

</tbody>
</table>

谢谢, 里亚兹

1 个答案:

答案 0 :(得分:2)

不要过于复杂化(为便于阅读而分成多行):

tr

我们在第二个单元格中找到span元素Name文字span,第五个文字CRM文字input。获取相应的行后,我们将找到{{1}}元素。

不需要像@BenChand建议的那样往下走树。