文本包含两个不同的字符串? WebDriver C#[关闭]

问题描述 投票:-2回答:3

我试图断言两个或更多字符串是否明显。我的代码目前只查找“好”。有没有办法寻找“好”或“坏”?

    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");
    }
}
c# selenium-webdriver contains assert
3个回答
1
投票

如果可能有两个以上的有效字符串,我会使用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条件。


1
投票

在尝试查找字符串时,始终将字符串变量设置为大写或小写。由于区分大小写,当文本为“GoOd”时,您将找不到匹配的“Good”

or

你也可以只在一个“if”语句中输入,如果你只是想通过使用它来查找是否有任何一个

if(conf.Text.ToUpper().Contains("GOOD")){
//do something
}
else if(conf.Text.ToUpper().Contains("BAD")){
//do something else
}

0
投票

||是OR运算的运算符

if(conf.Text.ToUpper().Contains("GOOD") || conf.Text.ToUpper().Contains("BAD")){
//do something for both cases
}

PD:停止你正在做的事情并查看语言文档,你需要了解你在做什么。

© www.soinside.com 2019 - 2024. All rights reserved.