【问题标题】:Xpath to select particular cell in a table depending on the other column text in the same rowXpath 根据同一行中的其他列文本选择表中的特定单元格
【发布时间】:2016-03-10 14:15:19
【问题描述】:

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

表格中有一行文字“姓名”在第 2 列,文字“crm”在第 4 列。 我想从该行中选择 Checkbox,而 Checkbox 位于 第 1 列

但还有另一行,第 2 列中包含文本 “名称”,第 4 列中包含文本 “escr”。我还有 2 行带有相同格式的文本 "Address"

我想选中第 2 列 包含文本 "Name"第 4 列 包含文本 "crm" 的复选框

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

我启动了一个 Xpath,它获取 column 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 名称文本在那里):

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

谢谢,

【问题讨论】:

    标签: python-2.7 selenium xpath selenium-webdriver


    【解决方案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 中并找到文本“名称”
    • 然后使用轴祖先选择当前节点的所有祖先,在col1中找到类型为checkbox的输入字段

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-02-08
      • 1970-01-01
      • 2014-02-06
      • 2018-03-16
      • 1970-01-01
      • 2018-03-23
      • 1970-01-01
      • 2012-11-01
      相关资源
      最近更新 更多