Consider a text containing both single and double quotes. I am storing it in a JS literal string
const sampleText = `Uses the "crow's foot" notation rather than classic ER notation to depict a relationship`
I need to find the div element which contains this text. I am using document.evaluate function
const xpath = `//div[text()="${sampleText}"]`
document
.evaluate(xpath, document, null, XPathResult.ANY_TYPE, null)
.iterateNext();
But the function call throws not a valid XPath.
Tried adding escaped blackslash \\ before double quotes " in sampleText but no luck.
Failed attempt:
const sampleText = `Uses the \\"crow's foot\\" notation rather than classic ER notation to depict a relationship`
Error:
Failed to execute 'evaluate' on 'Document': The string '//div/text()="Uses the \"crow's foot\" notation rather than classic ER notation to depict a relationship"' is not a valid XPath expression.
Also other answers couldn't help as my string contains both double and single quotes and need code in JS
Found a solution. Use the
concatfunction and separate the quotes. Wrote a simple function to take a string and out a literal string to be used inconcat(outString)Explanation: xpath doesn't support escaping characters , thus the only option is to use
double quoteswhen string containssingle quotesand vice versa. But we can use theconcatoperator to split the string by any 1 type of quote you decide and wrap it in another. In above function I am splitting bysingle quote.Also
.works better thantext()when finding xpath element via text inside it. Thus from sample in the question,Tip: The quotes on output string from
xConcatStringwill get confusing but its a comma separated list of string and each string element is enclosed in double quotes