Selenium to check text in a list

2019-07-25 17:24发布

I have a list of elements as shown in the code. And I am changing it to Array. Before my application starts, the user will enter some text in a text box.

C# Code:

IList<IWebElement> namelist = driver2.FindElements(By.TagName("SPAN"));
namelist.ToArray();

What I want to do is : to check whether text entered by the user is present in that list.

Suppose the user enters "String Text", then I need selenium to check whether "String Text" is present in this list.

1条回答
贪生不怕死
2楼-- · 2019-07-25 17:36

Just convert your array of elements to List and then verify presence of text with Any function from System.Linq namespace:

// Get text entered by the user
string checkText = "String Text";

if (namelist.ToList().Any(span => span.Text.Contains(checkText)))
{
    // List contains text
}
else
{
    // List does not contain text
}
查看更多
登录 后发表回答