Can't handle dynamic xpath

Hello everybody,

I have an issue related to dynamic xpath.
I started with the below code and can’t find out my element, I always have “Unable to locate element”.
For all my test I waited for page load ~5-20 seconds before doing my code and always see my element in inspector.
String s = "SIF UNDEFINED / Naema Duo 30"
String xpath = ‘//span[contains(text(),"’+ s +‘")]’
Try to print “xpath”, I have a right String I want => try to search with this xpath, I can find it in web inspector

After searching on forum I used “String.format” for my case :

String s = "SIF UNDEFINED / Naema Duo 30"
String xpath = ‘//span[contains(text(),"’+ s +‘")]’
The println is correct, i have my xpath string but still have “Unable to locate element”.

I tried the last solution with my xpath directly
String s = "SIF UNDEFINED / Naema Duo 30"
String xpath = ‘//span[contains(text(),"’+ s +‘")]’
driver.findElement(By.xpath(“//span[contains(text(),‘SIF UNDEFINED / Naema Duo 30’)]”)).isDisplayed()
And I have the same result “Unable to locate element”

Have some one have this problem like me, or have some solution for my case, please help me.

Many thanks

It is impossible for others to solve this without the target web page in hand on their browser.

Please share the URL of your target if it is publicly accessible.

If it is not public, please save the HTML source of the target page. Attach the html file here to disclose it to us.

1 Like

Here is a part of HTML of my case and and in the capture, i noted with a red case where I want my xpath point to.

html.html (6.5 KB)

Is it just a part? If you have a larger HTML as a whole, please show the whole. Do not trim.

Katalon would look at the whole of the page first. If you need to write a locator which forcus a particular part in the larger whole, we need to see the whole HTML.

ok I see,. Updated with full html

html.html (349.9 KB)

Thank you for the HTML you shared.

I looked at the html.html file. I looked for a string “Naema”. I found 4 <div> elements like the following:

<div class="teaser">Vous avez une alerte Vous avez une alerte 25/02/2023 20:15:29 7430 SIF UNDEFINED / Naema Duo 30 CODE…</div>

However, your original post showed your XPath to be

//span[contains(text(), '...Naema...')]

This XPath wants to find some <span> elements. I believe that the HTML has no such <span> element.

Your xpath is wrong. You will inevitably get an error “Unable to locate element”. The solution for you is to correct your locator so that it fits the target HTML source code.

1 Like

Hello Kazurayam,

I just verify my html file and I didn’t see my xpath too.
My xpath is correct But It’s found in an ‘iframe’, and this iframe has another html document (you can see it in the following capture)

In web inspector :

In html file that I sent you :

And here is my correct document html witch contains my xpath //span[contains(text(),“SIF UNDEFINED / Naema Duo 30”)].

html.html (12.4 KB)

Your target HTML has a <iframe>. You want to select some <span> elements out of the HTML inside the <iframe>. Have you found out how?

Maybe you need WebUI.switchFrame:

Have a look at

1 Like

I got it. Thank you @kazurayam