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("&", "&");
text.Replace("'", "'");
text.Replace(@"""", """);
text.Replace("<", "<");
text.Replace(">", ">");
return text.ToString();
}