Customizing xpath generation

The website i am trying to test using Katalon Studio generates id that are partialy static and partialy dynamic:

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
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?

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)

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


Doubtful but I don’t know.

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

Ah, cool. Thanks Kaz.

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.

1 Like

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

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

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

We did something similar to generate test objects.

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