Riaz Ladhani
Riaz Ladhani

Reputation: 4062

Xpath to select particular cell in a table depending on the other column text in the same row

I have a HTML table with some columns and rows.

There is a row in the table with the text "Name" in column 2 and the text "crm" in column 4. I would like to select the Checkbox from this row and the Checkbox is in column 1.

But there is an another row with the text "Name" in column 2 and the text "escr" in column 4. I also have 2 rows with text "Address" in the same format.

I would like to select the checkbox where column 2 has text "Name" and column 4 has text "crm"

How can I build the XPath to get the correct checkbox?

I have an Xpath started, it gets all the rows where column 4 has the text "crm"

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

I have also tried the following XPath, it does not work.

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

the HTML is (if scroll to the middle the 2 Name text is there):

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

Thanks,

Upvotes: 1

Views: 980

Answers (1)

Riaz Ladhani
Riaz Ladhani

Reputation: 4062

I have just managed to work it out. I have constructed the following 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"]
  • First locate the text "crm" anywhere within the tr, td, div and span tag.
  • Then using the axis ancestor select all ancestors of the current node, in col2 and locate the text "Name"
  • Then using the axis ancestor select all ancestors of the current node, in col1 locate the input field of type checkbox

Upvotes: 1

Related Questions