Can we get the xpath of an element through a text if it’s unique.
//div[text() = 'Unique string']
It didn’t work. Element not found.
Then the string is incorrect.
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:
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?
This expression is wrong, it would not work.
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.
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.
It worked actually. Thanks for replying.
I got to know a lot of new things with this post. You guys are very supportive.
Just as an FYI, the expression works fine:
normalize-space(.) are functionally equivalent. See https://developer.mozilla.org/en-US/docs/Web/XPath/Functions/normalize-space. The String argument is optional. If omitted, the text is found within the context of the current node, making the ‘.’ redundant:
Thank you, I didn’t know it