# Get Xpath through text

**URL:** <https://forum.katalon.com/t/get-xpath-through-text/17417>\
**Category:** Web Testing\
**Tags:** katalon-studio\
**Created:** [January 9, 2019, 10:19am UTC](https://forum.katalon.com/t/get-xpath-through-text/17417 "2019-01-09T10:19:16Z")\
**Posts on this page:** 14\
**Page:** 1

<div class="post-metadata">

**Author:** ![kavyavijaya](https://avatars.discourse-cdn.com/v4/letter/k/ec9cab/32.png) [@kavyavijaya](https://forum.katalon.com/u/kavyavijaya)\
**Post date:** [January 9, 2019, 10:19am UTC](https://forum.katalon.com/t/get-xpath-through-text/17417/1 "2019-01-09T10:19:17Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Marek\_Melocik](https://dub1.discourse-cdn.com/katalon/user_avatar/forum.katalon.com/marek_melocik/32/371_2.png) [@Marek\_Melocik](https://forum.katalon.com/u/Marek_Melocik)\
**Post date:** [January 9, 2019, 10:37am UTC](https://forum.katalon.com/t/get-xpath-through-text/17417/2 "2019-01-09T10:37:39Z")

</div>

Of course.

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

---

<div class="post-metadata">

**Author:** ![kavyavijaya](https://avatars.discourse-cdn.com/v4/letter/k/ec9cab/32.png) [@kavyavijaya](https://forum.katalon.com/u/kavyavijaya)\
**Post date:** [January 12, 2019, 3:04pm UTC](https://forum.katalon.com/t/get-xpath-through-text/17417/3 "2019-01-12T15:04:14Z")

</div>

It didn’t work. Element not found.

---

<div class="post-metadata">

**Author:** ![Marek\_Melocik](https://dub1.discourse-cdn.com/katalon/user_avatar/forum.katalon.com/marek_melocik/32/371_2.png) [@Marek\_Melocik](https://forum.katalon.com/u/Marek_Melocik)\
**Post date:** [January 12, 2019, 3:14pm UTC](https://forum.katalon.com/t/get-xpath-through-text/17417/4 "2019-01-12T15:14:45Z")

</div>

Then the string is incorrect. 🙂

---

<div class="post-metadata">

**Author:** ![Brandon\_Hein](https://dub1.discourse-cdn.com/katalon/user_avatar/forum.katalon.com/brandon_hein/32/1092_2.png) [@Brandon\_Hein](https://forum.katalon.com/u/Brandon_Hein)\
**Post date:** [January 12, 2019, 6:02pm UTC](https://forum.katalon.com/t/get-xpath-through-text/17417/5 "2019-01-12T18:02:50Z")

</div>

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’]

---

<div class="post-metadata">

**Author:** ![ThanhTo](https://dub1.discourse-cdn.com/katalon/user_avatar/forum.katalon.com/thanhto/32/697_2.png) [@ThanhTo](https://forum.katalon.com/u/ThanhTo)\
**Post date:** [January 13, 2019, 6:20am UTC](https://forum.katalon.com/t/get-xpath-through-text/17417/6 "2019-01-13T06:20:53Z")

</div>

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

> [@Marek\_Melocik](#):
>
> //div[text() = ‘Unique string’]

or @Brandon_Hein’s example

> [@Brandon\_Hein](#):
>
> //\*[normalize-space()=‘my text’]

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

Have a nice day !

---

<div class="post-metadata">

**Author:** ![kavyavijaya](https://avatars.discourse-cdn.com/v4/letter/k/ec9cab/32.png) [@kavyavijaya](https://forum.katalon.com/u/kavyavijaya)\
**Post date:** [January 13, 2019, 11:31am UTC](https://forum.katalon.com/t/get-xpath-through-text/17417/7 "2019-01-13T11:31:37Z")

</div>

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.  
WebUI.verifyElementVisible(findTestObject(‘Pixy’))  
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?  
Regards  
Kavya Ankem

---

<div class="post-metadata">

**Author:** ![kazurayam](https://dub1.discourse-cdn.com/katalon/user_avatar/forum.katalon.com/kazurayam/32/333_2.png) [@kazurayam](https://forum.katalon.com/u/kazurayam)\
**Post date:** [January 13, 2019, 2:28pm UTC](https://forum.katalon.com/t/get-xpath-through-text/17417/8 "2019-01-13T14:28:15Z")

</div>

> [@kavyavijaya](#):
>
> //\*[normalize-space()=‘Ratatouille’]

This expression is wrong, it would not work.

Rather

```auto
//*[normalize-space(.)='Ratatouille']

```

or

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

```

---

<div class="post-metadata">

**Author:** ![ThanhTo](https://dub1.discourse-cdn.com/katalon/user_avatar/forum.katalon.com/thanhto/32/697_2.png) [@ThanhTo](https://forum.katalon.com/u/ThanhTo)\
**Post date:** [January 13, 2019, 2:29pm UTC](https://forum.katalon.com/t/get-xpath-through-text/17417/9 "2019-01-13T14:29:29Z")

</div>

Hi @kavyavijaya.

I tried **//\*[normalize-space()=‘Ratatouille’]** on this [page](https://www.pixar.com/feature-films-launch) (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.

 ![Untitled](https://europe1.discourse-cdn.com/katalon/original/2X/c/ce59e3131a8f21d1cffdb670782d90de17b42692.png)

---

<div class="post-metadata">

**Author:** ![kavyavijaya](https://avatars.discourse-cdn.com/v4/letter/k/ec9cab/32.png) [@kavyavijaya](https://forum.katalon.com/u/kavyavijaya)\
**Post date:** [January 13, 2019, 4:26pm UTC](https://forum.katalon.com/t/get-xpath-through-text/17417/10 "2019-01-13T16:26:28Z")

</div>

Hey,  
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.

Regards  
Kavya Ankem

---

<div class="post-metadata">

**Author:** ![kavyavijaya](https://avatars.discourse-cdn.com/v4/letter/k/ec9cab/32.png) [@kavyavijaya](https://forum.katalon.com/u/kavyavijaya)\
**Post date:** [January 13, 2019, 4:28pm UTC](https://forum.katalon.com/t/get-xpath-through-text/17417/11 "2019-01-13T16:28:37Z")

</div>

Hey Marek  
It worked actually. Thanks for replying.

---

<div class="post-metadata">

**Author:** ![kavyavijaya](https://avatars.discourse-cdn.com/v4/letter/k/ec9cab/32.png) [@kavyavijaya](https://forum.katalon.com/u/kavyavijaya)\
**Post date:** [January 13, 2019, 4:29pm UTC](https://forum.katalon.com/t/get-xpath-through-text/17417/12 "2019-01-13T16:29:57Z")

</div>

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

Regards  
Kavya Ankem

---

<div class="post-metadata">

**Author:** ![Brandon\_Hein](https://dub1.discourse-cdn.com/katalon/user_avatar/forum.katalon.com/brandon_hein/32/1092_2.png) [@Brandon\_Hein](https://forum.katalon.com/u/Brandon_Hein)\
**Post date:** [January 14, 2019, 1:34am UTC](https://forum.katalon.com/t/get-xpath-through-text/17417/13 "2019-01-14T01:34:23Z")

</div>

> [@kazurayam](#):
>
> > //\*[normalize-space()=‘Ratatouille’]
> 
> This expression is wrong, it would not work.
> 
> Rather
> 
> ```auto
> //*[normalize-space(.)='Ratatouille']
> 
> ```
> 
> or
> 
> ```auto
> //*[contains(text(), 'Ratatouille')]
> 
> ```

Just as an FYI, the expression works fine: `normalize-space()` and `normalize-space(.)` are functionally equivalent. See [normalize-space - XPath | MDN](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:

With argument:

![image](https://europe1.discourse-cdn.com/katalon/original/2X/c/cc6b1f46a8acc43233a5dc97d92fce49dbdc0d11.png)

Without argument:

![image](https://europe1.discourse-cdn.com/katalon/original/2X/a/a4d889ffb4081f3d3b16d5fb44505a71478203be.png)

---

<div class="post-metadata">

**Author:** ![kazurayam](https://dub1.discourse-cdn.com/katalon/user_avatar/forum.katalon.com/kazurayam/32/333_2.png) [@kazurayam](https://forum.katalon.com/u/kazurayam)\
**Post date:** [January 14, 2019, 2:23am UTC](https://forum.katalon.com/t/get-xpath-through-text/17417/14 "2019-01-14T02:23:29Z")

</div>

Thank you, I didn’t know it
