1

The variable may come across characters that are used in Xpath, respectively, there is a syntax error, how can you win ?

Example variable (this is just an example, the characters in it can be different):

string textElementa = "it ' is" 

Search element by Xpath:

IWebElement elem = diver.FindElement(By.XPath("//div[normalize-space()='" + textElementa + "']"));

The Html code to:

<!DOCTYPE html>
<html>
<head>
    <title></title>
</head>
<body>
    <div>
        <span>it</span> <span>'</span> <span>is</span>
    </div>
</body>
</html>

I've tried all this, but it doesn't work for me. https://stackoverflow.com/questions/35854525

I also tried this:

    public static string ToXPath(string value)
    {
       return "'" + XmlEncode(value) + "'";
    }

    public static string XmlEncode(string value)
    {
       StringBuilder text = new StringBuilder(value);
       text.Replace("&", "&amp;");
       text.Replace("'", "&apos;");
       text.Replace(@"""", "&quot;");
       text.Replace("<", "&lt;");
       text.Replace(">", "&gt;");
       return text.ToString();
    }
Vipz
  • 207
  • 1
  • 7
  • Have you tried string textElementa = @"it ' is" – Moe Ghafari Sep 13 '18 at 16:08
  • Possible duplicate of [How to escape single quote within string variable in NCalc.Expression - Backslash didn't work](https://stackoverflow.com/questions/35854525/how-to-escape-single-quote-within-string-variable-in-ncalc-expression-backslas) – JeffC Sep 13 '18 at 16:19

0 Answers0