Contains function not working?

675 views
Skip to first unread message

TraumaHound

unread,
May 26, 2011, 11:51:05 AM5/26/11
to FirePath
I have entered an xPath expression to evaluate against a DOM and it
should only "highlight" one div element. However it appears to be
highlighting all div elements.

Here is the string: //div[contains(.,'You can change your password')]

Here is what FirePath is showing:

http://screencast.com/t/9rridgxkxSd

This doesn't seem correct. I would think it would only highlight the
div that actually contains that string. When I use the string in a
Selenium script as a locator argument, it works.

Please advise.

Pierre Tholence

unread,
May 26, 2011, 12:36:55 PM5/26/11
to fire...@googlegroups.com
Hi,

If you look at XPath specification the contains function is defined the following way (http://www.w3.org/TR/xpath/#function-contains):
Function: boolean contains(string, string)
The contains function returns true if the first argument string contains the second argument string, and otherwise returns false.

In your specific case the first argument of the contains function is the context node (the dot). So the XPath engine has to convert it to a string before it can run the contains function.

The conversion is done implicitly using the string function which for a node returns its string value (as defined here http://www.w3.org/TR/xpath/#function-string).
In your case the nodes are elements (divs) so so there string value is the following (http://www.w3.org/TR/xpath/#element-nodes):
The string-value of an element node is the concatenation of the string-values of all text node descendants of the element node in document order.

As an example, to see what the string value looks like you can evaluate the following XPath in the same page the screenshot was taken:
string(.//div[@id='div_loginContainer'])

As you can see the resulting string does contain 'You can change your password'

So the result you are getting is expected.

Now I think the XPath expression you are looking for here is:
//div[contains(text(),'You can change your password')]

This matches the divs that have a child text node which contains 'You can change your password'

Hope this helps,
Regards,
Pierre

TraumaHound

unread,
May 26, 2011, 7:02:56 PM5/26/11
to FirePath
Thanks a ton for the clarification. It behaves exactly as you
describe (or as the spec describes). And thanks to your information,
I learned a few more "tricks" for my Selenium scripts.

Cheers,
Mason

On May 26, 12:36 pm, Pierre Tholence <pierre.thole...@gmail.com>
wrote:
> Hi,
>
> If you look at XPath specification the contains function is defined the
> following way (http://www.w3.org/TR/xpath/#function-contains):
>
> > Function: boolean contains(string, string)
> > The contains function returns true if the first argument string contains
> > the second argument string, and otherwise returns false.
>
> In your specific case the first argument of the contains function is the
> context node (the dot). So the XPath engine has to convert it to a string
> before it can run the contains function.
>
> The conversion is done implicitly using the *string* function which for a
> node returns its string value (as defined herehttp://www.w3.org/TR/xpath/#function-string).
Reply all
Reply to author
Forward
0 new messages