0

I am automating a web system using selenium webdriver, in multiple scenarios I need to assert that given data in in one line (tr) of the web page.

For example, I need to assert that there is in the system a line (tr) with these data: Data 1, Data 2, Data 3, Data 4

And my page is structured like this:

<tr>
    <td>Data 1</td>
    <td>Data 2</td>
    <td>Data 3</td>
    <td><span class='something'>Data 4</span></td>
<tr>

I wrote this xpath hoping it would return the table row:

.//tr[td[text()='Data 1']]/parent::tr/td[text()='Data 2']/parent::tr/td[text()='Data 3']/parent::tr/td[text()='Data 4']

It works until Data 3 but in Data 4 does not work

Rogger Fernandes
  • 805
  • 4
  • 14
  • 28
  • Duplicate of [Testing text() nodes vs string values in XPath](http://stackoverflow.com/questions/34593753/testing-text-nodes-vs-string-values-in-xpath) – kjhughes Oct 20 '16 at 14:16

3 Answers3

0

It's not working because there is a <span> element embedded in 4-th <td>.

You should try

.//tr[td[text()='Data 1']]/.../parent::tr/td[.='Data 4']

or

.//tr[td[text()='Data 1']]/.../parent::tr/td[span[text()='Data 4']]
Andersson
  • 51,635
  • 17
  • 77
  • 129
0

I would want a solution that is generic in point of match any level inside my td

You should try using . instead of text() which would be a generic solution as below :-

.//tr[td[.='Data 1'] and td[.='Data 2'] and td[.='Data 3'] and td[.='Data 4']]

Or if you want leading and trailing whitespace as well try using normalize-space() function of the xpath as below :-

.//tr[td[normalize-space()='Data 1'] and td[normalize-space()='Data 2'] and td[normalize-space()='Data 3'] and td[normalize-space()='Data 4']]
Community
  • 1
  • 1
Saurabh Gaur
  • 23,507
  • 10
  • 54
  • 73
0

Try using this XPath, I think it will work in your case:

.//tr[.//*[.='Data 1'] and [.='Data 2'] and [.='Data3'] or [.='Data 4']]

It will search for a tr node that has child contains these text.

Viet Pham
  • 214
  • 2
  • 5