Element dynamically changing

I need to find an element which changes each time the page is loaded.
The prefix “ABC_ID” and suffix “login” remains the same.

I’ve tried xpath with “contains” and “starts-with” / “substring”, but no success.

return $browser.waitForAndFindElement(By.xpath("//[contains(@id,‘login’)]"), DefaultTimeout).then(function(el) {

return $browser.waitForAndFindElement(By.xpath("//*[starts-with(@id, ‘ABC_ID’) and substring(@id, string-length(@id) - string-length(‘login’) +1) = ‘login’]")).then(function (el) {

Any idea of what is wrong?

Hi @aligil

The $driver.By.xpath locator should support contains and starts-with. It’s hard to say why it’s not working with the information provided. I’m happy to take a closer look, do you have a link to the monitor in question?


I know this is an older issue, but I found it on google after trying to figure out a solution to Katalon Recorder not supporting “assertElementPresent” export to NR Synthetics language.

My contains pattern is not as advanced as yours, but I recently came up with this solution to locate if specific images exist on a page:

 return $browser.waitForAndFindElement(By.xpath("//*[contains(@src,'/img/v2/Ok.png')]"), DefaultTimeout)}).then(function() {

Maybe you were just missing something simple like the * before your contains bracket. For me, I had to trim off the trailing element tags that Katalon Recorder put into the XPath for the image I was trying to validate.

This article helped me a lot in figuring out how to use XPaths (not sure if it was available at the time of your post):

Thanks for letting us know @william.handford. :slightly_smiling_face: It’s always helpful when people offer any info that they have, even on older posts like this. It really helps out other users who may currently have the same issue.