gowtham gowtham - 3 months ago 50
C# Question

How to convert WebElement to string?

Here is the code i'm trying with . I'm using C# basic if else statement to compare.

IWebElement findtitle = driver.FindElement(By.ClassName("heading3"));

if (title = findtitle.Text)
{
Console.WriteLine("Test Pass");
}
else
{
Console.WriteLine("Test Fail");
}

Guy Guy
Answer

You assigning the value to title, not comparing it. Use == to compare

if (title == findtitle.Text)

If you want to compare using ignore case use

if (findtitle.Text.IndexOf(title, StringComparison.OrdinalIgnoreCase) != -1)

This will check if findtitle text contains the text of title.