MoreScratch
MoreScratch

Reputation: 3083

Return all rows in a table that has specific column header text using Xpath

I have the following HTML table in a page:

<table>
    <tbody>
        <tr>
            <th>Birth Date</th>
        </tr>
    </tbody>
</table>

I have the following Xpath:

//table[//th[contains(text(), "Birth Date")]]/tr

I get rows from all tables in the page, not the one with the Birth Date header.

What am I missing?

Upvotes: 0

Views: 806

Answers (2)

tomjn
tomjn

Reputation: 5389

If I understand what you want then I think you are close. I think you want

'//table[.//th[contains(text(), "Birth Date")]]//tr'

(note the . before //th to query with respect to the table rather than the entire document).

You could also write this as

'//table[contains(.//th/text(), "Birth Date")]//tr'

Upvotes: 1

gangabass
gangabass

Reputation: 10666

One possible reason that you have table based layout on this page: you have one big main table, each cell contains another tables and so on.

You need to be more specific to find your target table:

//table[ tbody/tr/th[contains(text(), "Birth Date")] ]/tbody/tr

Upvotes: 1

Related Questions