Sunday, June 30, 2013

xpath and selenium : locate a node using exact text match

Problem

I want to locate a Element on a Web Page using text
I know there is a method name contains to do so
ex
tr[contains(.,'hello')]/td
But problem is if i have two elements name hello and hello1 then this function does not work properly
is there any other method like contains for exact string match for locating elements

Solutions
1
Well, your problem is that you are searching text into the tr (which is not correct anyway) and this cause a problem to the function contains which cannot accept a list of text. Try to use this location path instead. It should retrieve what you want.
//tr/td[contains(./text(),"hello")]
This location path will retrieve a set of node on which you have to iterate to get the text. You can try to append the
/text()
but this will cause (at least on my test) a result that is a string which is a concatenation of all the matched strings.

2.
tr[.//text()='hello']/td
this will select all td child elements of all tr elements having a child with exactly 'hello' in it. Such an XPath still sounds odd to me.
I believe that this makes more sense:
tr/td[./text()='hello']
because it selects only the td that contains the text.
3.
It all depends on what your HTML actually contains, but your tr[contains(.,'hello')]/td XPath selector means "the first cell of the first row that contains the string 'hello' anywhere within it" (or, more accurately, "the first TD element in the TR element that contains the string 'hello' anywhere within it", since Selenium has no idea what the elements involved really do). That's why it's getting the wrong result when there are rows containing "hello" and "hello1" - both contain "hello".
The selector tr[. ='hello']/td would be more accurate, but it's a little unusual (because HTML TR elements aren't supposed to contain text - the text is supposed to be in TH or TD elements within the TR), and it probably won't work (because text in any other cells would break the comparison). You probably want tr[td[.='hello']]/td, which means "the first TD element contained in the TR element that contains a TD element that has the string 'hello' as it's complete text".

No comments:

Post a Comment