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

Python 2.7 Xpath根据同一行中的其他列文本选择表中的特定单元格,python-2.7,selenium,xpath,selenium-webdriver,Python 2.7,Selenium,Xpath,Selenium Webdriver,我有一个包含一些列和行的HTML表 表中有一行,第2列为“名称”,第4列为“crm”。 我想从此行中选择复选框,而复选框位于第1列 但是在第2列中有另一行的文本“Name”,在第4列中有文本“escr”。我还有两行文字“地址”,格式相同 我想选中复选框,其中第2列有文本“名称”,第4列有文本“crm” 如何构建XPath以获得正确的复选框 我启动了一个Xpath,它获取列4中包含文本“crm”的所有行 我还尝试了以下XPath,但它不起作用 //table[@id="data_configura

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

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

但是在第2列中有另一行的文本“Name”,在第4列中有文本“escr”。我还有两行文字“地址”,格式相同

我想选中复选框,其中第2列
有文本
“名称”,第4列有文本“crm”

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

我启动了一个Xpath,它获取列4中包含文本“crm”的所有行

我还尝试了以下XPath,但它不起作用

//table[@id="data_configuration_mappings_ct_fields_body"]//tr[.//div[text()="crm"] and .//div[text()="Name"]]//input
HTML是(如果滚动到中间,则有两个名称文本):


地址
地址
客户关系管理
主要
ADD1+ADD2+ADD3+ADD4+PCODE
客户关系管理
DM
电话
电话
客户关系管理
主要
电话
客户关系管理
DM
ACVSEQ
ACVSEQ
客户关系管理
主要
移动+标题
客户关系管理
DM
出生日期
出生日期
客户关系管理
主要
出生日期
客户关系管理
DM
名称
名称
客户关系管理
主要
头衔+姓名+网名+性别
客户关系管理
DM
名称
名称
escr
主要
头衔+姓名+网名+性别
ESCR
DM

谢谢,

我刚刚设法解决了这个问题。我构建了以下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”
  • 然后使用axis祖先在col2中选择当前节点的所有祖先并定位文本“Name”
  • 然后使用轴祖先选择当前节点的所有祖先,在col1中找到类型为复选框的输入字段
<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>
//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"]