XPath strings do not have escape sequences. This
//ul[@id='xyz-ul']/li[contains(text(),'Épargne en vue d\'un objectif précis')]
is just as invalid as this
//ul[@id='xyz-ul']/li[contains(text(),'Épargne en vue d'un objectif précis')]
would be.
It depends on the circumstances if you can mitigate this issue, or if you are out of luck.
In the context of a testing framework you might very well be out of luck, but you need to show more of your code to get a better answer.
There are two ways to get single quotes into a string in XPath.
Use double quotes for the string. Then you can use single quotes inside (and vice versa).
//ul[@id='xyz-ul']/li[contains(text(),"Épargne en vue d'un objectif précis")]
When this is not feasible, like in your situation, because you never know whether a user-supplied value contains single- or double quotes, you must use the concat()
XPath function like this:
//ul[@id='xyz-ul']/li[contains(text(), concat('Épargne en vue d', "'", 'un objectif précis'))]
The second version looks difficult, but it can in fact be built reliably by splitting and joining the input string, like this
- Input string is
Épargne en vue d'un objectif précis
- Splitting at
'
gives Épargne en vue d
and un objectif précis
- Joining with
', "'", '
gives Épargne en vue d', "'", 'un objectif précis
- Wrapping with
concat('
and ')
gives concat('Épargne en vue d', "'", 'un objectif précis')
,
which is exactly the string you can put into an XPath expression with a placeholder, like this one:
${dropdown_selection_first_part_xpath} = //ul[@id='xyz-ul']/li[contains(text(),
${dropdown_selection_second_part_xpath} = )]
${dropdown_xpath} = //div[@id='xyz-widget']/div/div[2]/div[1]
${escaped_input_purpose_of_account} = (somehow to the 4 steps above)
${dropdown_selection_xpath} = catenate SEPARATOR= ${dropdown_selection_first_part_xpath} ${escaped_input_purpose_of_account} ${dropdown_selection_second_part_xpath}
This approach works for all inputs (empty string, strings with no quotes, strings with any mix of single- and double quotes) and always creates syntactically valid XPath expression that does what you expect.