Xpath根据同一行中的其他列文本选择表中的特定单元格

时间:2016-03-10 14:15:19

标签: python-2.7 selenium xpath selenium-webdriver

我有一个包含一些列和行的HTML表。

表格中有一行,第2列中的文字为“名称”,第4列中的文字为“crm”。 我想从此行中选择复选框复选框位于第1列中。

但是第二列中的文字“名称”和第4列中的文字“escr”还有另一行。我还有两行文字“地址”,格式相同。

我想选中第2列包含文字“名称”的复选框,第4列包含文字“crm”

如何构建XPath以获取正确的复选框?

我已启动Xpath,它会获取第4列包含文字“crm”

的所有行
//table[@id="data_configuration_mappings_ct_fields_body"]//tr//td//div//span[text()="crm"]

我也尝试了以下XPath,它不起作用。

//table[@id="data_configuration_mappings_ct_fields_body"]//tr[.//div[text()="crm"] and .//div[text()="Name"]]//input

HTML是(如果向中间滚动2 Name文本):

<table id="data_configuration_mappings_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-222" style="outline-style:none;" tabindex="0">
                <input type="checkbox" tabindex="-1"/>
            </div>
        </td>
        <td class="GJPPK2LBEG GJPPK2LBGG">
            <div __gwt_cell="cell-gwt-uid-223" 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 GJPPK2LBGG">
            <div __gwt_cell="cell-gwt-uid-224" 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 GJPPK2LBGG">
            <div __gwt_cell="cell-gwt-uid-225" 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">
            <div __gwt_cell="cell-gwt-uid-226" style="outline-style:none;">
                <span class="" title="Main"
                      style="white-space:nowrap;overflow:hidden;text-overflow:ellipsis;empty-cells:show;display:block;padding-right: 1px;">Main</span>
            </div>
        </td>
        <td class="GJPPK2LBEG GJPPK2LBGG">
            <div __gwt_cell="cell-gwt-uid-227" style="outline-style:none;">
                <span class="" title="ADD1 + ADD2 + ADD3 + ADD4 + PCODE"
                      style="white-space:nowrap;overflow:hidden;text-overflow:ellipsis;empty-cells:show;display:block;padding-right: 1px;">ADD1 + ADD2 + ADD3 + ADD4 + PCODE</span>
            </div>
        </td>
        <td class="GJPPK2LBEG GJPPK2LBGG">
            <div __gwt_cell="cell-gwt-uid-228" 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">
            <div __gwt_cell="cell-gwt-uid-229" 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>
        <td class="GJPPK2LBEG GJPPK2LBGG GJPPK2LBBH">
            <div __gwt_cell="cell-gwt-uid-230" 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>
    </tr>
    <tr class="GJPPK2LBEH" __gwt_subrow="0" __gwt_row="1">
        <td class="GJPPK2LBEG GJPPK2LBFH GJPPK2LBHG">
            <div __gwt_cell="cell-gwt-uid-222" style="outline-style:none;">
                <input type="checkbox" tabindex="-1"/>
            </div>
        </td>
        <td class="GJPPK2LBEG GJPPK2LBFH">
            <div __gwt_cell="cell-gwt-uid-223" 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-224" 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-225" 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 GJPPK2LBFH">
            <div __gwt_cell="cell-gwt-uid-226" style="outline-style:none;">
                <span class="" title="Main"
                      style="white-space:nowrap;overflow:hidden;text-overflow:ellipsis;empty-cells:show;display:block;padding-right: 1px;">Main</span>
            </div>
        </td>
        <td class="GJPPK2LBEG GJPPK2LBFH">
            <div __gwt_cell="cell-gwt-uid-227" 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-228" 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">
            <div __gwt_cell="cell-gwt-uid-229" 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>
        <td class="GJPPK2LBEG GJPPK2LBFH GJPPK2LBBH">
            <div __gwt_cell="cell-gwt-uid-230" 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>
    </tr>
    <tr class="GJPPK2LBFG" __gwt_subrow="0" __gwt_row="2">
        <td class="GJPPK2LBEG GJPPK2LBGG GJPPK2LBHG">
            <div __gwt_cell="cell-gwt-uid-222" style="outline-style:none;">
                <input type="checkbox" tabindex="-1"/>
            </div>
        </td>
        <td class="GJPPK2LBEG GJPPK2LBGG">
            <div __gwt_cell="cell-gwt-uid-223" 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 GJPPK2LBGG">
            <div __gwt_cell="cell-gwt-uid-224" 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 GJPPK2LBGG">
            <div __gwt_cell="cell-gwt-uid-225" 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">
            <div __gwt_cell="cell-gwt-uid-226" style="outline-style:none;">
                <span class="" title="Main"
                      style="white-space:nowrap;overflow:hidden;text-overflow:ellipsis;empty-cells:show;display:block;padding-right: 1px;">Main</span>
            </div>
        </td>
        <td class="GJPPK2LBEG GJPPK2LBGG">
            <div __gwt_cell="cell-gwt-uid-227" style="outline-style:none;">
                <span class="" title="MOBILE + TITLE"
                      style="white-space:nowrap;overflow:hidden;text-overflow:ellipsis;empty-cells:show;display:block;padding-right: 1px;">MOBILE + TITLE</span>
            </div>
        </td>
        <td class="GJPPK2LBEG GJPPK2LBGG">
            <div __gwt_cell="cell-gwt-uid-228" 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">
            <div __gwt_cell="cell-gwt-uid-229" 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>
        <td class="GJPPK2LBEG GJPPK2LBGG GJPPK2LBBH">
            <div __gwt_cell="cell-gwt-uid-230" 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>
    </tr>
    <tr class="GJPPK2LBEH" __gwt_subrow="0" __gwt_row="3">
        <td class="GJPPK2LBEG GJPPK2LBFH GJPPK2LBHG">
            <div __gwt_cell="cell-gwt-uid-222" style="outline-style:none;">
                <input type="checkbox" tabindex="-1"/>
            </div>
        </td>
        <td class="GJPPK2LBEG GJPPK2LBFH">
            <div __gwt_cell="cell-gwt-uid-223" 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 GJPPK2LBFH">
            <div __gwt_cell="cell-gwt-uid-224" 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 GJPPK2LBFH">
            <div __gwt_cell="cell-gwt-uid-225" 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">
            <div __gwt_cell="cell-gwt-uid-226" style="outline-style:none;">
                <span class="" title="Main"
                      style="white-space:nowrap;overflow:hidden;text-overflow:ellipsis;empty-cells:show;display:block;padding-right: 1px;">Main</span>
            </div>
        </td>
        <td class="GJPPK2LBEG GJPPK2LBFH">
            <div __gwt_cell="cell-gwt-uid-227" 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 GJPPK2LBFH">
            <div __gwt_cell="cell-gwt-uid-228" 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">
            <div __gwt_cell="cell-gwt-uid-229" 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>
        <td class="GJPPK2LBEG GJPPK2LBFH GJPPK2LBBH">
            <div __gwt_cell="cell-gwt-uid-230" 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>
    </tr>
    <tr class="GJPPK2LBFG" __gwt_subrow="0" __gwt_row="4">
        <td class="GJPPK2LBEG GJPPK2LBGG GJPPK2LBHG">
            <div __gwt_cell="cell-gwt-uid-222" style="outline-style:none;">
                <input type="checkbox" tabindex="-1"/>
            </div>
        </td>
        <td class="GJPPK2LBEG GJPPK2LBGG">
            <div __gwt_cell="cell-gwt-uid-223" 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-224" 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-225" 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">
            <div __gwt_cell="cell-gwt-uid-226" style="outline-style:none;">
                <span class="" title="Main"
                      style="white-space:nowrap;overflow:hidden;text-overflow:ellipsis;empty-cells:show;display:block;padding-right: 1px;">Main</span>
            </div>
        </td>
        <td class="GJPPK2LBEG GJPPK2LBGG">
            <div __gwt_cell="cell-gwt-uid-227" style="outline-style:none;">
                <span class="" title="TITLE + FNAME + SNAME + GENDER"
                      style="white-space:nowrap;overflow:hidden;text-overflow:ellipsis;empty-cells:show;display:block;padding-right: 1px;">TITLE + FNAME + SNAME + GENDER</span>
            </div>
        </td>
        <td class="GJPPK2LBEG GJPPK2LBGG">
            <div __gwt_cell="cell-gwt-uid-228" 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">
            <div __gwt_cell="cell-gwt-uid-229" 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>
        <td class="GJPPK2LBEG GJPPK2LBGG GJPPK2LBBH">
            <div __gwt_cell="cell-gwt-uid-230" 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>
    </tr>
    <tr class="GJPPK2LBEH" __gwt_subrow="0" __gwt_row="5">
        <td class="GJPPK2LBEG GJPPK2LBFH GJPPK2LBHG">
            <div __gwt_cell="cell-gwt-uid-222" style="outline-style:none;">
                <input type="checkbox" tabindex="-1"/>
            </div>
        </td>
        <td class="GJPPK2LBEG GJPPK2LBFH">
            <div __gwt_cell="cell-gwt-uid-223" 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 GJPPK2LBFH">
            <div __gwt_cell="cell-gwt-uid-224" 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 GJPPK2LBFH">
            <div __gwt_cell="cell-gwt-uid-225" 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 GJPPK2LBFH">
            <div __gwt_cell="cell-gwt-uid-226" style="outline-style:none;">
                <span class="" title="Main"
                      style="white-space:nowrap;overflow:hidden;text-overflow:ellipsis;empty-cells:show;display:block;padding-right: 1px;">Main</span>
            </div>
        </td>
        <td class="GJPPK2LBEG GJPPK2LBFH">
            <div __gwt_cell="cell-gwt-uid-227" style="outline-style:none;">
                <span class="" title="TITLE + FNAME + SNAME + GENDER"
                      style="white-space:nowrap;overflow:hidden;text-overflow:ellipsis;empty-cells:show;display:block;padding-right: 1px;">TITLE + FNAME + SNAME + GENDER</span>
            </div>
        </td>
        <td class="GJPPK2LBEG GJPPK2LBFH">
            <div __gwt_cell="cell-gwt-uid-228" 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 GJPPK2LBFH">
            <div __gwt_cell="cell-gwt-uid-229" 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>
        <td class="GJPPK2LBEG GJPPK2LBFH GJPPK2LBBH">
            <div __gwt_cell="cell-gwt-uid-230" 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>
    </tr>
    </tbody>
</table>

谢谢,

1 个答案:

答案 0 :(得分:1)

我刚刚设法解决了这个问题。我构建了以下Xpath:

//table[@id="data_configuration_mappings_ct_fields_body"]//tr//td//div//span[text()="crm"]/ancestor::tr[1]//td[2]//div//span[text()="Name"]/ancestor::tr[1]/td[1]//input[@type="checkbox"]
  • 首先在tr,td,div和span标记内的任何位置找到文本“crm”。
  • 然后使用轴祖先选择当前节点的所有祖先,在col2中找到文本“Name”
  • 然后使用轴祖先选择当前节点的所有祖先,在col1中找到类型复选框的输入字段