user1198316
user1198316

Reputation: 267

XPath matching text in a table - Ruby - Nokigiri

I have a table that looks like this

<table cellpadding="1" cellspacing="0" width="100%" border="0">
  <tr>
      <td colspan="9" class="csoGreen"><b class="white">Bill Statement Detail</b></td>
  </tr>
  <tr style="background-color: #D8E4F6;vertical-align: top;">
      <td nowrap="nowrap"><b>Bill Date</b></td>
      <td nowrap="nowrap"><b>Bill Amount</b></td>

      <td nowrap="nowrap"><b>Bill Due Date</b></td>
      <td nowrap="nowrap"><b>Bill (PDF)</b></td>
  </tr>
</table>

I am trying to create the XPATH to find this table where it contains the test Bill Statement Detail. I want the entire table and not just the td.

Here is what I have tried so far:

page.parser.xpath('//table[contains(text(),"Bill")]')

page.parser.xpath('//table/tbody/tr[contains(text(),"Bill Statement Detail")]')

Any Help is appreciated

Thanks!

Upvotes: 1

Views: 1277

Answers (3)

JXU
JXU

Reputation: 67

The suggested codes don't work well if the match word is not in the first row. See the related post Find a table containing specific text

Upvotes: 0

Zach Young
Zach Young

Reputation: 11178

Your first XPath example is the closest in that you're selecting table. The second example, if it ever matched, would select tr—this one will not work mainly because, according to your example, the text you want is in a b node, not a tr node.

This solution is as vague as I could make it, because of *. If the target text will always be under b, change it to descendant::b:

//table[contains(descendant::*, 'Bill Statement Detail')]

This is as specific, given the example, as I can make:

//table[tr[1]/td/b['Bill Statement Detail']]

Upvotes: 1

Jayson Lorenzen
Jayson Lorenzen

Reputation: 575

You might want

//table[contains(descendant::text(),"Bill Statement Detail")]

Upvotes: 0

Related Questions