Get Xpath through text

Can we get the xpath of an element through a text if it’s unique.

Of course.

//div[text() = 'Unique string']

1 Like

It didn’t work. Element not found.

Then the string is incorrect. :slight_smile:

You can get an element by its text, even if that text isn’t unique (if multiple instances of the text node exist, the first one found is returned by default, but you can always add an index in there to get the appropriate one).

We need to see the relevant part of the HTML you are working with to give you an XPath that works though.

For the fun of it, I’ll try giving you an XPath like Marek did. Try:

//*[normalize-space()=‘my text’]

Hi @kavyavijaya

It is worth mentioning the difference between normalize-space(.) and normalize-space(text()), because the explanation may contain many relevant pieces of information that you need.

First, for a string a, normalize-space(a) removes leading and trailing white spaces of a, it also replaces consecutive ( more than 2 instances ) white spaces inside a with one instance of white space.

Then onto the difference between text() and .(the dot). The dot represents the current node (which in normalize-space(.) is the node obtained by the XPath prior to normalize-space(.). The current node will be converted into a text node, which is all of the child text nodes concatenated. While with text() will only retrieve the direct text node of the current node.

This means that if the current node contains the text ‘Hi’ and it also contains other html elements which contain text like ‘Hello’ and ‘world’, normalize-space(.) will return ‘Hi Hello world’ and normalize-space(text()) will return ‘Hi’.

This may be important when you use @Marek_Melocik

or @Brandon_Hein’s example

and may be an explanation of why your XPath isn’t working.

Have a nice day !


Thanks for your suggestion and the knowledge share. It will always be helpful.
I am trying to identify the element with text Ratatouille in pixar animation website first page.
Pixy is the object with xpath set as //*[normalize-space()=‘Ratatouille’]
unable to find element
I think I should not set xpath so what object property do i have to set?
Kavya Ankem

This expression is wrong, it would not work.




//*[contains(text(), 'Ratatouille')]

Hi @kavyavijaya.

I tried //*[normalize-space()=‘Ratatouille’] on this page (which I assume was the page you referred to ?) and it detected an element.

If you’re using Chrome, then Ctrl + Shift + I will open Inspection Tool. On the Element tab, you can press Ctrl + F and paste your XPath into the search bar, if the XPath works then the element will be highlighted.

1 Like

Thanks for the quick reply. The problem was with verify element visible as the text is visible after scrolling(I think). When I changed it to verify element present it worked.

Kavya Ankem

Hey Marek
It worked actually. Thanks for replying.

Thanks everyone.
I got to know a lot of new things with this post. You guys are very supportive.

Kavya Ankem

Just as an FYI, the expression works fine: normalize-space() and normalize-space(.) are functionally equivalent. See The String argument is optional. If omitted, the text is found within the context of the current node, making the ‘.’ redundant:

With argument:


Without argument:


Thank you, I didn’t know it

1 Like