# Customizing xpath generation

**URL:** <https://forum.katalon.com/t/customizing-xpath-generation/15801>\
**Category:** Web Testing\
**Tags:** web-testing, katalon-studio\
**Created:** [December 20, 2018, 12:36pm UTC](https://forum.katalon.com/t/customizing-xpath-generation/15801 "2018-12-20T12:36:29Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![Michal.Pachucki](https://avatars.discourse-cdn.com/v4/letter/m/e56c9b/32.png) [@Michal.Pachucki](https://forum.katalon.com/u/Michal.Pachucki)\
**Post date:** [December 20, 2018, 12:36pm UTC](https://forum.katalon.com/t/customizing-xpath-generation/15801/1 "2018-12-20T12:36:29Z")

</div>

Hello,  
The website i am trying to test using Katalon Studio generates id that are partialy static and partialy dynamic:  
//button[@id=‘staticId1:dynamicId:staticId2’]/span

But there are never two elements that have the same staticId1/2 and different dynamicId.  
So i would like to somehow generate xpath  
//button[starts-with(@id, ‘staticId1’) and endswith(‘staticId2’, @id)]/span  
Or at least  
//button[@id$=‘staticId2’]/span  
Because staticId2 is usualy unique.

I know I can edit the xpath manually, but I will have hundreds of them, so I would prefer that it would be automatic.

Is there a way of suppying my own xpath generator?

---

<div class="post-metadata">

**Author:** ![Russ\_Thomas](https://dub1.discourse-cdn.com/katalon/user_avatar/forum.katalon.com/russ_thomas/32/11884_2.png) [@Russ\_Thomas](https://forum.katalon.com/u/Russ_Thomas)\
**Post date:** [December 20, 2018, 12:48pm UTC](https://forum.katalon.com/t/customizing-xpath-generation/15801/2 "2018-12-20T12:48:40Z")

</div>

I believe starts-with is xpath 2.0 (and Katalon uses xpath 1.0) (See [Test Object with xpath selector using matches() function never works](http://forum.katalon.com/t/test-object-with-xpath-selector-using-matches-function-never-works/11822))

CSS supports “$=” – depending on how Katalon supplies the CSS setting to the host browser, you could try this in your Test Object definition:

```auto
button[id$=staticId2]

```

> [@](#):
>
> Is there a way of suppying my own xpath generator?

Doubtful but I don’t know.

---

<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:** [December 20, 2018, 12:50pm UTC](https://forum.katalon.com/t/customizing-xpath-generation/15801/3 "2018-12-20T12:50:37Z")

</div>

> [@Russ\_Thomas](#):
>
> starts-with is xpath 2.0

starts-with function is defined by xpath1.0 but ends-with function is defined by xpath2.0.

---

<div class="post-metadata">

**Author:** ![Russ\_Thomas](https://dub1.discourse-cdn.com/katalon/user_avatar/forum.katalon.com/russ_thomas/32/11884_2.png) [@Russ\_Thomas](https://forum.katalon.com/u/Russ_Thomas)\
**Post date:** [December 20, 2018, 12:52pm UTC](https://forum.katalon.com/t/customizing-xpath-generation/15801/4 "2018-12-20T12:52:14Z")

</div>

Ah, cool. Thanks Kaz.

---

<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:** [December 20, 2018, 12:53pm UTC](https://forum.katalon.com/t/customizing-xpath-generation/15801/5 "2018-12-20T12:53:32Z")

</div>

Write a Groovy code which creates a Test Object on the fly.

It will read an existing Test Object with xpath `//button[@id=‘staticId1:dynamicId:staticId2’]/span`

It will translate the xpath into another one like `//button[starts-with(@id,'staticId1') and ends-with(@id,'staticId2')]/span`

Then it will create a new Test Object using the generated xpath.

XPath2.0 `ends-with()` function is not available to us. But alternative (very verbose) xpath expression can do the same query

Let me work out for a few days for developing a solution.

---

<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:** [December 20, 2018, 1:27pm UTC](https://forum.katalon.com/t/customizing-xpath-generation/15801/6 "2018-12-20T13:27:55Z")

</div>

alternative to ends-with() in xpath1.0 will be like this:

```auto
//*[substring(@id, string-length(@id) - string-length('staticId2') +1) = 'staticId2']

```

I will use `substring()` and `string-length()` functions which are available in XPath1.0.

---

<div class="post-metadata">

**Author:** ![Harold\_Owen](https://dub1.discourse-cdn.com/katalon/user_avatar/forum.katalon.com/harold_owen/32/417_2.png) [@Harold\_Owen](https://forum.katalon.com/u/Harold_Owen)\
**Post date:** [December 20, 2018, 10:31pm UTC](https://forum.katalon.com/t/customizing-xpath-generation/15801/7 "2018-12-20T22:31:41Z")

</div>

We did something similar to generate test objects.

> [@Generate Test Objects from Command-line](http://forum.katalon.com/t/generate-test-objects-from-command-line/14706):
>
> For our Katalon workflow we have had success generating test objects from the command line, outside of Katalon Studio. We wrote a Java utility to do this, although you could use any scripting language, such as Python, Groovy, Powershell etc. It is based on the assumption that all of the test objects are identified by HTML ID attribute. We try to insist that all page elements have unique IDs in order to avoid identifying objects using complex XPath that are likely to change over time. We use …

---

<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:** [December 25, 2018, 2:48am UTC](https://forum.katalon.com/t/customizing-xpath-generation/15801/8 "2018-12-25T02:48:33Z")

</div>

I have created another post where I tried to propose a solution to this issue.

- [Transforming Test Object on the fly](http://forum.katalon.com/t/transforming-test-object-on-the-fly/15934)
