1

For HTML tables on web page I am using the following XPath:

/tr/td[2]/.[contains(text(),'Some')]  

This works fine in all the case but it also match 'Something'.

/tr/td[2]/.[normalize-space(text()) = 'Some']

doesn't work in all the cases. Can somebody comment on what's wrong with latter XPath?

kjhughes
  • 106,133
  • 27
  • 181
  • 240
user2764578
  • 61
  • 3
  • 10
  • There's nothing wrong with the latter path. Please provide an example of HTML code where the given XPath doesn't work as you'd expect. Otherwise it's hardly possible to help you. – Thomas W Jul 07 '16 at 19:07
  • @ThomasW: Actually, both XPath's are malformed: Probably OP meant to use `*` rather than `.`. But beyond that, I agree that the problem is probably not about `normalize-space()`. See [my answer below](http://stackoverflow.com/a/38253476/290085) for details. – kjhughes Jul 07 '16 at 19:21
  • @user2764578: Asking 12 questions over 2 years without accepting a single answer is a sign you should read [**How does accepting an answer work?**](http://meta.stackoverflow.com/q/5234/234215) and probably also **[ask]**. Thanks. – kjhughes Jul 08 '16 at 02:32

1 Answers1

0

Your problem doesn't likely involve normalize-space() but rather one of two common text/string matching areas of confusion:

Text node vs string value

text() matches text nodes.

//td[contains(text(), 'Some')] will match this

<td>Some text</td>

but not

<td><b>Some text</b></td>

To match the latter too, use //td[contains(., 'Some')] instead. This will check that the string value of td contains the string "Some".

For more details, see XPath text() = is different than XPath . =

String contains vs string equals

Note also that contains() tests for substring containment. If you want string equality, use the = operator against string:

//td[. = 'Some']

Will match

<td><b>Some</b></td>

but not

<td><b>Some text</b></td>

Be aware of the difference.

Community
  • 1
  • 1
kjhughes
  • 106,133
  • 27
  • 181
  • 240
  • I have Some .. So I think '.' won't work here, I tried [normalize-space(text()) = 'Some'] ..but it's not working – user2764578 Jul 08 '16 at 19:54
  • Stop guessing and asking us to guess. Provide a true [mcve], or Thomas W is right: It's not possible to help you. – kjhughes Jul 08 '16 at 20:25