该变量可能会分别遇到Xpath中使用的字符,出现语法错误,您怎么能赢?
示例变量(这只是一个示例,其中的字符可以不同):
string textElementa = "it ' is"
通过Xpath搜索元素:
IWebElement elem = diver.FindElement(By.XPath("//div[normalize-space()='" + textElementa + "']"));
要执行以下操作的HTML代码
<!DOCTYPE html>
<html>
<head>
<title></title>
</head>
<body>
<div>
<span>it</span> <span>'</span> <span>is</span>
</div>
</body>
</html>
我已经尝试了所有这一切,但是对我来说不起作用。 https://stackoverflow.com/questions/35854525
我也尝试过:
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();
}