Home > Software engineering >  How to write an XPath to select text with a quote & comma character?
How to write an XPath to select text with a quote & comma character?

Time:12-21

How can I select a XPath that contains a text with both quote & comma character?

I have to find an element if the text contains =Yes, It's OK

My XPath does not save in ranorex tool even though if i put the text inside the double quotes like below

//span[text()="Yes, It's OK"]

So how can I save this xpath that uses "".in Ranorex

CodePudding user response:

You have to escape the apostrophe, but not the comma:

//span[text()="Yes, It's OK"]

You can have a look at a list of predefined XML entities over at WikiPedia.

CodePudding user response:

your use of double-quotes is correct. if

//span[text()="Yes, It's OK"]

doesn't match, it might be because the xpath engine has a lowercase bug (i have encountered PHP DOMXPath libxml2 systems where it would only match lowercase text even for text with uppercase characters, never quite figured out the problem there), or it might be because the text has hidden whitespace you're not aware of, maybe it actually has a hard-to-spot whitespace at the start or the end? anyway maybe contains() will work:

//span[contains(text(),"Yes, It's OK"]

or.. if it has the lowercase-issue i have encountered in the wild once before, maybe this will work:

//span[contains(text(),"yes, it's ok"]

(that really shouldn't be required, but i have been in that situation once before, and that was years ago, and that was... probably php5)

  • Related