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.

2
  • I tried //td[contains(text(),'Network')] which works. But I want to use all three words in text i.e 'Create Network Container' Jun 23, 2014 at 9:34
  • Given relevant portion of the HTML markup you're working with is exactly the same as posted here, your XPath should match that <td> element
    – har07
    Jun 23, 2014 at 10:45

2 Answers 2

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']
1
  • +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, 2017 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.

2

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.