Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using XPATH to search text containing whitespace

Tags:

xpath

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.

like image 453
user3766763 Avatar asked Jun 23 '14 09:06

user3766763


People also ask

How does XPath handle spaces?

If an element has spaces in its text or in the value of any attribute, then to create an xpath for such an element we have to use the normalize-space function. It removes all the trailing and leading spaces from the string. It also removes every new tab or lines existing within the string.

How do you write XPath for class with spaces?

XPath("//*[contains(@class,'right-body-2')]//td[contains(concat(' ',normalize-space(@class),' '), ' ac ')]/a/@href"));

How do I trim a space in XPath?

As I say, XPath is not a string-handling mechanism; it cannot remove spaces. It is concerned solely with the retrieval of data. Anything you want to do TO that data must be done separately, and currently we don't know what language you're using to do that in.


2 Answers

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']
like image 155
helderdarocha Avatar answered Oct 11 '22 13:10

helderdarocha


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.

like image 20
mareckmareck Avatar answered Oct 11 '22 15:10

mareckmareck