# Xpath writing example

**URL:** <https://forum.katalon.com/t/xpath-writing-example/25932>\
**Category:** Web Testing\
**Tags:** katalon-studio\
**Created:** [May 21, 2019, 9:15pm UTC](https://forum.katalon.com/t/xpath-writing-example/25932 "2019-05-21T21:15:14Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![vweerasinghe](https://avatars.discourse-cdn.com/v4/letter/v/977dab/32.png) [@vweerasinghe](https://forum.katalon.com/u/vweerasinghe)\
**Post date:** [May 21, 2019, 9:15pm UTC](https://forum.katalon.com/t/xpath-writing-example/25932/1 "2019-05-21T21:15:14Z")

</div>

Hi Katalon community,

I want to locate a td element by the text inside; however the text is separated by i elements inside td element.

```
<td> "first"

 <i></i>

  "second"

 <i></i>

 "third" 

  </td>

```

There are multiple tags on the webpage. If I write //td[contains(text(),‘first’)], it locates all of them. If I write the xpath such that it include the texts “first”, “second” and “third”, it would locate the exact td element. I tried //td[contains(text(),‘first’)][contains(text(),‘second’)][contains(text(),‘third’)]  
However, this xpath does not work. Is there a way to write the xpath that includes all the text?

Any help would be appreciated.  
Thanks : D

---

<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:** [May 21, 2019, 9:27pm UTC](https://forum.katalon.com/t/xpath-writing-example/25932/2 "2019-05-21T21:27:37Z")

</div>

```auto
//td[contains(text(),‘first’)][contains(text(),‘second’)][contains(text(),‘third’)]

```

I find this XPath is OK. This is not a problem.

> [@vweerasinghe](#):
>
> However, this xpath does not work.

I guess you have a problem somewhere else, other than XPath syntax, in your Test Case script. For example, you do not wait for the page to load before trying to find the `<td>` element inside it.

---

<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:** [May 21, 2019, 9:44pm UTC](https://forum.katalon.com/t/xpath-writing-example/25932/3 "2019-05-21T21:44:48Z")

</div>

Just use the ‘and’ operator:

`//td[contains(text(),‘first’) and contains(text(),‘second’) and contains(text(),‘third’)]`

---

<div class="post-metadata">

**Author:** ![vweerasinghe](https://avatars.discourse-cdn.com/v4/letter/v/977dab/32.png) [@vweerasinghe](https://forum.katalon.com/u/vweerasinghe)\
**Post date:** [May 22, 2019, 12:43am UTC](https://forum.katalon.com/t/xpath-writing-example/25932/4 "2019-05-22T00:43:11Z")

</div>

hmm, for some reason, the ‘and’ operator that @Brandon_Hein suggested does not work. And the syntax suggested by @kazurayam also does not work. I tried copy and pasting both approaches and switching out the text.Maybe if I give more details; something can be found. I will also see if any of the devs in my company have any idea.

```
  <td>Workflow <i class="icon-caret-right muted">::before</i> Record <i class="icon-caret-right muted">::before</i> New</td>
```

---

<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:** [May 22, 2019, 5:08am UTC](https://forum.katalon.com/t/xpath-writing-example/25932/5 "2019-05-22T05:08:09Z")

</div>

The XPath that @Brandon_Hein suggested is equivalent to the one I suggested. Both failed. Then it proves that you have a problem in your test case other than XPath. For example, you do not wait for the page to load before trying to find the `<td>` element inside it. Do you do verifyElementPresent() in your test case? Please share the code of your test case.

---

<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:** [May 22, 2019, 4:48pm UTC](https://forum.katalon.com/t/xpath-writing-example/25932/6 "2019-05-22T16:48:10Z")

</div>

Give this one a try:

`//td[contains(normalize-space(), 'Workflow') and contains(normalize-space(), 'Record') and contains(normalize-space(), 'New')]`

The idea here is to use the normalize-space() function as a predicate. This function considers not only the first text node found, but all child text nodes of the specified element.

---

<div class="post-metadata">

**Author:** ![vweerasinghe](https://avatars.discourse-cdn.com/v4/letter/v/977dab/32.png) [@vweerasinghe](https://forum.katalon.com/u/vweerasinghe)\
**Post date:** [May 23, 2019, 1:26am UTC](https://forum.katalon.com/t/xpath-writing-example/25932/7 "2019-05-23T01:26:10Z")

</div>

Thanks @Brandon_Hein and @kazurayam! : D  
The normalize-space approach worked for me 👍🏽

---

<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:** [May 24, 2019, 1:51am UTC](https://forum.katalon.com/t/xpath-writing-example/25932/8 "2019-05-24T01:51:07Z")

</div>

This is an interest post for me. It motivated me to study in depth how XPath functions text(), contains() and normalize-space() work.

I have made a project on Github.

> **[GitHub - kazurayam/XPathWritingExample: How XPath functions text(),...](https://github.com/kazurayam/XPathWritingExample)**
>
> How XPath functions text(), contains() and normalize-space() work - GitHub - kazurayam/XPathWritingExample: How XPath functions text(), contains() and normalize-space() work

The sample code is here: [XPathWritingExample/Script1558585003703.groovy at master · kazurayam/XPathWritingExample · GitHub](https://github.com/kazurayam/XPathWritingExample/blob/master/Scripts/TC1/Script1558585003703.groovy)

This code emits the folloing output:

```auto
tdWithMixedContent is '<td>Workflow <i class="icon-caret-right muted">::before</i> Record <i class="icon-caret-right muted">::before</i> New</td>'

# XPath text() functions returns a NodeList when applied to an HTML Element with mixed content
'count(//td/text())' returns 3.0
'//td/text()[1]' returns 'Workflow '
'//td/text()[2]' returns ' Record '
'//td/text()[3]' returns ' New'

# XPath contains(nodelist,pattern) function silently chooses only the 1st node of the nodelist given as 1st argument
'//td[contains(text(), "Workflow")]' returns true
'//td[contains(text(), "Record")]' returns false !!!
'//td[contains(text(), "New")]' returns false !!!

# you can choose a node out of the nodelist returned by text()
'//td[contains(text()[1], "Workflow")]' returns true
'//td[contains(text()[2], "Record")]' returns true
'//td[contains(text()[3], "New")]' returns true

# normalize-space() function scans the descendent nodes of the target element recursively for text contents, concatinates them to a string, normalizes white space characters
'normalize-space(//td)' returns 'Workflow ::before Record ::before New'

# it is convenient to call contains() with normalize-space() as the 1st argument
'//td[contains(normalize-space(), "Workflow")]' returns true
'//td[contains(normalize-space(), "Record")]' returns true
'//td[contains(normalize-space(), "New")]' returns true
'//td[contains(normalize-space(), "Workflow") and contains(normalize-space(), "Record") and contains(normalize-space(), "New")]' returns true
'//td[contains(normalize-space(.), "Workflow")][contains(normalize-space(.), "Record")][contains(normalize-space(.), "New")]' returns true

2019-05-24 10:27:30.121 INFO c.k.katalon.core.main.TestCaseExecutor - END Test Cases/TC1

```
