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 +‘")]’
driver.findElement(By.xpath(xpath)).isDisplayed()
`
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 +‘")]’
println(String.format(xpath,s))
driver.findElement(By.xpath(String.format(xpath,s))).isDisplayed()
`
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.
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.
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.
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)