1

I am trying to locate all elements from a search result page and put them in a list. First problem is to find all the elements. I have no problem finding a specific element:

driver.FindElement(By.CssSelector("#event_395634"));

OR:

driver.FindElement(By.Id("event_395634"));

But how do I find every element starting with "#event_" or "event_"?

And how do I put them in a list? Is this a start?:

List<IWebElement> eventList = new List<IWebElement>();
events = driver.FindElements(By.?("Magic code");

....and then what? Or am I way off? I intend to loop through the list at a later stage.

SamKar
  • 31
  • 5

1 Answers1

0

There is a CSS selector which selects all elements whose attribute starts with a given string.
You can use it with By.CssSelector.

var events = driver.FindElements(By.CssSelector("[id^=event_]"));

It should work.

Yeldar Kurmangaliyev
  • 33,467
  • 12
  • 59
  • 101
  • Yes, it did! Thank you! Do you know the answer to the second question as well? About the list? Now I´m this far: `var events = driver.FindElements(By.CssSelector("[id^=event_]")); events.ToList(); foreach (object o in events) { System.Diagnostics.Debug.WriteLine(events); }` – SamKar Oct 19 '17 at 10:01