Mugen
Mugen

Reputation: 1579

How can I choose an element using xpath that has 2 X 2 kinds of structure?

This is what the wonderful HTML table in Confluence looks like:

<tr>
  <td> key1
  </td>

  <td> key2
  </td>

  <td> value1
  </td>

  <td> value2
  </td>
</tr>

I need an xpath that can identify a row based on key1 + key2.

This would be easy by itself right? Now here's the complication with confluence:

Each row can either contain the value directly inside td, or, it can have a td > span tag which will contain the value.

In other words, each row can be in either of these formats:

<td> text
</td>

OR,

<td>
  <span>
    text
  </span>
</td>

Here's where I'm currently at:

(//tr[descendant::td[text()='key1' or descendant::span[text()='key1'] ] and descendant::td[text()='key2' or descendant::span[text()='key2'] ]])

This doesn't work. It doesn't catch anything.

PS: I've totally not spent half the day trying to grab the different rows! The real problem is more than this but I've solved the second half. The row selection isn't working.

Upvotes: 1

Views: 50

Answers (2)

Mugen
Mugen

Reputation: 1579

I solved this! OMG, I can't believe I solved it after putting in hours. It's a very simple solution actually! :)

//*[text() = 'key1']/ancestor::tr[descendant::*[text() = 'key2']]

This selects the row according to the logical expression { (td=key1 OR td/span=key1) && (td=key2 OR td/span=key2) )

Upvotes: 0

Andersson
Andersson

Reputation: 52665

Try below XPath to match row by values of two cells:

//tr[normalize-space(td)="key1" and normalize-space(td[2])="key2"]

Upvotes: 2

Related Questions