Erik Erik - 1 year ago 93
C# Question

How to get all elements into list or string with Selenium?

I've tried using webelement(and IWebElement) and string lists, but i keep getting errors. How I do get a list or strings of all the elements text by classname? I have all Selenium references. Do i need some java.util dll? Should I implement a foreach loop?

IList<IWebElement> all = new IList<IWebElement>();
all = driver.FindElements(By.ClassName("comments")).Text;

Error: Cannot create an instance of the abstract class or interface 'System.Collections.Generic.IList'

Error: 'System.Collections.ObjectModel.ReadOnlyCollection' does not contain a definition for 'Text' and no extension method 'Text' accepting a first argument of type

Answer Source

You can get all of the element text like this:

IList<IWebElement> all = driver.FindElements(By.ClassName("comments"));

String[] allText = new String[all.Count];
int i = 0;
foreach (IWebElement element in all)
    allText[i++] = element.Text;