13

In

<tr>
  <td class="f4 trimJustl"
      valign="middle"
      style="color:#ffffff;"
      artcolor="#ffffff">Create Network Container</td>
</tr>

I want to find the td element where the text is equal to Create Network Container. I created the XPath

//td[text()='Create Network Container']

But it's not working. I also tried

//td[contains(text(),'Create Network Container')]

but this isn't working for me either.

blalasaadri
  • 5,990
  • 5
  • 38
  • 58
user3766763
  • 181
  • 1
  • 3
  • 8
  • I tried //td[contains(text(),'Network')] which works. But I want to use all three words in text i.e 'Create Network Container' – user3766763 Jun 23 '14 at 09:34
  • Given relevant portion of the HTML markup you're working with is exactly the same as posted here, your XPath should match that `` element – har07 Jun 23 '14 at 10:45

2 Answers2

33

It works by cutting and pasting your posted example. Your original source probably has tabs or other whitespace characters that don't match. Here are some alternatives:

1) Normalize spaces

//td[normalize-space(text()) = 'Create Network Container']

2) Compare using string value of td (this will also match Create <b>Network</b> Container)

//td[normalize-space(.) = 'Create Network Container']

3) Check for each word separately (ignores word order)

//td[contains(text(), 'Create') and contains(text(), 'Network') and contains(text(), 'Container')]

4) Strip whitespace, tabs, newlines, line-feeds, carriage returns and compare result:

//td[translate(text(), "  &#13;&#10;&#09;&#xa;", "") = 'CreateNetworkContainer']
helderdarocha
  • 23,209
  • 4
  • 50
  • 65
  • +1 Vote. Was dealing with an xpath that I was using "contains" and it wasn't finding text on the screen when I had a hyphen. The normalize-space helped me! Thanks! – IamBatman Jan 16 '17 at 21:45
1

Try this:

//td[matches(text(), '\s*Create\s+Network\s+Container\s*')]

To be honest this works for me in several evaluators I checked it in:

 //td[text() = 'Create Network Container']

Previous try was to match all potential space-like characters that might be there (perhaps it's not just a single whitespace there and that's why this simpliest solution doesn't give you proper results.

Community
  • 1
  • 1
mareckmareck
  • 1,560
  • 13
  • 18