I have researched and researched but nothing seems to work. Say I want an xpath with the text "Tony's Computer". I cannot do the following:
//*[contains(text(), 'Tony's Computer')]
for obvious reasons, as the ' between the y and s unquotes the text. But nor does this work:
//*[contains(text(), 'Tony\'s Computer')]
also
//*[contains(text(), 'Tony''s Computer')]
Is there a way to escape the apostrophe? I cannot find anything. The best I can do is the following with Java (assume textToLookFor has the text to look for):
String[] fields = textToLookFor.split("'");
String textx = "";
for (int indx = 0; indx < fields.length; indx++) {
String stext = fields[indx];
String cont = "contains(text(),'" + stext + "') ";
if (!textx.isEmpty()) {
textx += " and ";
}
textx += cont;
}
String xpath = "//*[" + textx + "]";
which basically does a
//*[contains(text(),'Tony') and contains(text(), 's Computer')]
which is I suppose good enough. However, it is not an exact match. It could find "Tony and Bob's Computer" for instance.
Has anyone else had success doing this? I cannot find any other solutions. By the way, when I find the element I can say String displayed = ele.getText(); and then check that displayed.contains(textToLookFor).
In this case I first need to do a textToLookFor = textToLookFor.replaceAll("\"", "\\\""); which escapes any double quotes.