Riaz Ladhani
Riaz Ladhani

Reputation: 4062

Selenium XPATH how can i get text out from table column 4 where text is in table column 2

I have a HTML table with some columns and rows. I am trying to find the text from column 5 and the text from column 3. The text I would like to find from col 5 is 101 THE BatCave|GOTHAM CITY| and the text 14 from col 3

To start I first find the text 14 from col 3 with the following XAPTH:

//table[@id="reporting_view_report_dg_main_body"]//tr//td[3]/div/span[@title="14"]

But I don't know how to get the text from column 5 also. I have tried:

//table[@id="reporting_view_report_dg_main_body"]//tr//td[3]/div/span[@title="14"] and td[5]//span[contains(text(), "101 THE BatCave|GOTHAM CITY|"])

//table[@id="reporting_view_report_dg_main_body"]//tr//td[3]/div/span[@title="14"]/following::td[5]

The HTML snippet is:

<table id="reporting_view_report_dg_main_body" cellspacing="0" style="table-layout: fixed; width: 100%; margin-bottom: 17px;">
<colgroup>
    <tbody>
        <tr class="GFNQNVHJM" __gwt_subrow="0" __gwt_row="0" />
        <tr class="GFNQNVHIN" __gwt_subrow="0" __gwt_row="1"/>
        <tr class="GFNQNVHJM" __gwt_subrow="0" __gwt_row="2"/>
        <tr class="GFNQNVHJM GFNQNVHAN" __gwt_subrow="0" __gwt_row="12"/>
        <td class="GFNQNVHIM GFNQNVHKM GFNQNVHLM GFNQNVHBN"/>
        <td class="GFNQNVHIM GFNQNVHKM GFNQNVHBN"/>
        <td class="GFNQNVHIM GFNQNVHKM GFNQNVHBN"/>
        <div __gwt_cell="cell-gwt-uid-319" style="outline-style:none;"/>
        <span title="14" style="white-space:nowrap;overflow:hidden;text-overflow:ellipsis;empty-cells:show;display:block;padding-right: 1px;">14</span>
    </div>
</td>
<td class="GFNQNVHIM GFNQNVHKM GFNQNVHBN">
    <td class="GFNQNVHIM GFNQNVHKM GFNQNVHBN">
        <div __gwt_cell="cell-gwt-uid-321" style="outline-style:none;">
            <span title="101 BatCave|GOTHAM CITY|" style="white-space:nowrap;overflow:hidden;text-overflow:ellipsis;empty-cells:show;display:block;padding-right: 1px;">101 THE BatCave|GOTHAM CITY|</span>
        </div>
    </td>
    <td class="GFNQNVHIM GFNQNVHKM GFNQNVHBN">
        <td class="GFNQNVHIM GFNQNVHKM GFNQNVHBN"/>
        <td class="GFNQNVHIM GFNQNVHKM GFNQNVHBN"/>
        <td class="GFNQNVHIM GFNQNVHKM GFNQNVHBN"/>
        <td class="GFNQNVHIM GFNQNVHKM GFNQNVHBN"/>
        <td class="GFNQNVHIM GFNQNVHKM GFNQNVHFN GFNQNVHBN"/>
    </tr>
    <tr class="GFNQNVHIN" __gwt_subrow="0" __gwt_row="13"/>
    <tr class="GFNQNVHJM" __gwt_subrow="0" __gwt_row="14"/>
</tbody>

Thanks, Riaz

Upvotes: 3

Views: 1721

Answers (2)

Saurabh Gaur
Saurabh Gaur

Reputation: 23805

This is impossible to combine both columns together using xpath and get their text combination. You can find only these two columns in the list and then combine their text through loop as below :

combineText = ""
columns = driver.find_element_by_id("reporting_view_report_dg_main_body").find_elements_by_xpath(".//span[@title = '14'] | .//span[@title = '101 BatCave|GOTHAM CITY|']")
for column in columns:
    combineText += column.text

print(combineText)

Upvotes: 1

alecxe
alecxe

Reputation: 473863

Not sure about the reliability of this approach in your particular case, but given what provided, you can get both elements by getting span elements with title attribute:

elements = driver.find_elements_by_xpath("//table[@id='reporting_view_report_dg_main_body']//span[@title]")
for element in elements:
    print(element.text)

Upvotes: 2

Related Questions