我试图断言两个或更多字符串是否明显。我的代码目前只查找“好”。有没有办法寻找“好”或“坏”?
public class Test
{
public static bool FindText()
{
var conf = Driver.Instance.FindElement(By.Id("foo"));
if (conf.Text.Contains("Good"))
{
return true;
}
throw new Exception("Text not found");
}
}
如果可能有两个以上的有效字符串,我会使用System.Linq
并检查数组的所有元素。
public class Test
{
public static bool FindText()
{
var stringsToFind = new [] { "Good", "Bad" };
var conf = Driver.Instance.FindElement(By.Id("foo"));
if (stringsToFind.Any(s => conf.Text.Contains(s))
{
return true;
}
throw new Exception("Text not found");
}
}
只检查两个元素我可能只是用第二个条件和qazxsw poi扩展if条件。
在尝试查找字符串时,始终将字符串变量设置为大写或小写。由于区分大小写,当文本为“GoOd”时,您将找不到匹配的“Good”
or
你也可以只在一个“if”语句中输入,如果你只是想通过使用它来查找是否有任何一个
if(conf.Text.ToUpper().Contains("GOOD")){
//do something
}
else if(conf.Text.ToUpper().Contains("BAD")){
//do something else
}
||是OR运算的运算符
if(conf.Text.ToUpper().Contains("GOOD") || conf.Text.ToUpper().Contains("BAD")){
//do something for both cases
}
PD:停止你正在做的事情并查看语言文档,你需要了解你在做什么。