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.
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.
XPath("//*[contains(@class,'right-body-2')]//td[contains(concat(' ',normalize-space(@class),' '), ' ac ')]/a/@href"));
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.
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(), " 	
", "") = 'CreateNetworkContainer']
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With