Xpath ID is changing how to set up dynamically

Hey Everyone,

I apologise I know this has been covered in other posts but I must admit im struggling to understand a little bit. I have a number of xpath Ids that are changing only slightly and the remainder of the ID should be able to identify the field. I have included some examples below. Im wondering is it possible to just wildcard a part of the Xpath ID so it matches on the remainder?

//[@id=“id-a837e4a7-01b8-4f82-a475-be9abd67e667-297-name-name.fieldControl-text-box-text”]
//
[@id=“id-a837e4a7-01b8-4f82-a475-be9abd67e667-1-name-name.fieldControl-text-box-text”]
//*[@id=“id-a837e4a7-01b8-4f82-a475-be9abd67e667-299-name-name.fieldControl-text-box-text”]

I have bolded the component of the Xpath that changes the remainder stays the same and should find a unique match. Is there a simple way to do this?

Use XPath builtin-functions starts-with and ends-with

//*[starts-with(@id,"id-a837e4a7-01b8-4f82-a475-be9abd67e667-"][ends-with(@id,"-name-name.fieldControl-text-box-text"]

Hey,

Sorry to be a pain, when I put this into xpath helper I get [INVALID XPATH EXPRESSION]. Would this be expected?

Katalon object spy also doesnt recognise this command.

Thanks

I think the double quotation character is problematic. The editor of this site silently translates a double quotation character " into something else and . Quite annoying.

Do not copy the posted text and paste it as XPath, which will be evaluated to be invalid.

Please type ", then it should work.

Hey,

Thanks for your help with this.

I can get the starts-with expression working by adding in a missing closing bracket ) and not including the ends-with.

//*[starts-with(@id,“id-a837e4a7-01b8-4f82-a475-be9abd67e667-”)]

however the ends-with doesn’t work at all even if I don’t include the starts with. I tried writing this way as well and it didn’t like that either.

//input[starts-with(@id,“id-a837e4a7-01b8-4f82-a475-be9abd67e667-”) and ends-with(@id,“name-name.fieldControl-text-box”)]

maybe its not supported in the version of xpath i’m using?

Ah, I was wrong.
The ends-with function is only available in XPath 2.0.
Browsers supports only XPath 1.0.
Therefore we can not use ends-with function in Katalon Studio.

You can write an alternative expression using substring and string-length function:

//*[starts-with(@id,"id-a837e4a7-01b8-4f82-a475-be9abd67e667-"][substring(@id, string-length(@id) - string-length('-name-name.fieldControl-text-box-text') +1) = '-name-name.fieldControl-text-box-text']
2 Likes

Hey Mate,

That did the trick thank you. I can use this for my other fields to! legend thanks :smiley:

I made a couple tweaks for anyone else reading this.

//*[starts-with(@id,'id-a837e4a7-01b8-4f82-a475-be9abd67e667-')][substring(@id, string-length(@id) - string-length('-name-name.fieldControl-text-box-text') +1) = '-name-name.fieldControl-text-box-text']

Please use the code formatting feature next time.

This will avoid confusion caused by silent translation of a double quotation character " into something else and

The folloing expression is too long; therefore error-prone and difficult to maintain.

//*[starts-with(@id,'id-a837e4a7-01b8-4f82-a475-be9abd67e667-')][substring(@id, string-length(@id) - string-length('-name-name.fieldControl-text-box-text') +1) = '-name-name.fieldControl-text-box-text']

You would want to parameterize the expression like this:

//*[starts-with(@id,'${PREFIX}')][substring(@id, string-length(@id) - string-length('${POSTFIX}') +1) = '${POSTFIX}']

The following post would tell you how to make a parameterised Test Object in Katalon Studio

1 Like

Hey,

I did this to fix any of the quote issues you talked about. I also added a closing bracket again and this identified the field correctly.

Would you prefer I update to " again?

Thanks

Up to you. If your test runs as you want, that’s fine.