Find list of Elements from Webpage in Selenium C#

In this tutorial, we are going to discuss How to extract all the web elements from the webpage.

Find list of Elements from Webpage in Selenium C#

For this, we are going to use a method called “FindElements”. When we use this method it returns us the list of web elements and if it is not able to locate its element then, in that case, it is going to return us an empty list.

But if we use the “FindElement” method in that case it is going to throw us “NoSuchElementException” and it is not able to locate the element.

But in the case of “FindElements”, it will not throw us an exception instead it will return us an empty list.

FindElements in Selenium C#

So here inside our App.config we will change the BrowserType to Chrome so we can see the interactions. And we are going to create one more directory called FindElements, inside this we will create a class called HandleElements which will be a public class.

[TestClass]
public class HandleElements
{
[TestMethod] 
public static void GetAllElements()
{
NavigationHelper.NavigateToUrl(ObjectRepository.Config.GetWebsite());
ReadOnlyCollection<IWebElement> elements= ObjectRepository.Driver.FindElements(By.XPath("//input"));
ReadOnlyCollection<IWebElement> elements= ObjectRepository.Driver.FindElements(By.Id("123"));
}
}

Inside this class, we are creating a method public void GetAllElements() so first of all we will use NavigationHelper to Navigate to the required page NavigationHelper.NavigateToUrl(ObjectRepository.Config.GetWebsite());

And here we will call the method so ObjectRepository.Driver.FindElements( ); and in brackets, the locator will be By.XPath.

So if we open our firebug and type the XPath as // input we can see here there are a total of 22 web elements that are matching with that XPath

so we will use the same XPath here also, in that case, it is going to return us the list of web element (By.XPath("//input"));

As we can see here, it is going to return us the ReadOnlyCollection and this collection will have <IWebElement> and this will be elements and we are going to specify the Id of which there are no elements present in the UI so let’s say (By.Id("123")); so we don’t have any element related to this Id.

We will put a breakpoint over ReadOnlyCollection<IWebElement> elements= ObjectRepository.Driver.FindElements(By.XPath("//input")); this line and build our solution and run the script in debug mode.

Output: so it has hit the debug point when we do a stepover we can see here the size of the list elements is 22, which means it is able to locate all the 22 web elements.

So we have all the 22 web elements matching with this XPath

and if we expand this list so every entry inside this is in the form of a web element that has these properties.

 

And if we do a stepover again so as we can see here we don’t have any element with this Id that’s why the size of the list is 0 which means an empty list.

Example to Find list of Elements from Webpage in Selenium C#

Now moving on let us suppose we want to extract all the Id of the elements with //input this XPath. So here we have already a list of those web elements all we need to do is call the foreach loop.

So here we will use the keyword called “var” when we use this keyword with the element or a variable it is going to automatically set the data type of this variable based on the assignment.

For example-

var ele= "string"; here the data type of this variable ele will become string and if we use var b=10;  so automatically the data type of this variable b become int.

So this particular keyword “var” automatically identifies the data type of the variable based on the assignment.

Do the modification in the previous program.

[TestClass] 
public class HandleElements 
{ 
[TestMethod] 
public static void GetAllElements() 
 { NavigationHelper.NavigateToUrl(ObjectRepository.Config.GetWebsite()); 
ReadOnlyCollection<IWebElement> elements= ObjectRepository.Driver.FindElements(By.XPath("//input")); ReadOnlyCollection<IWebElement> elements= ObjectRepository.Driver.FindElements(By.Id("123")); 
foreach(var ele in elements)
  {
  Console.WriteLine("Id:{0}",ele.GetAttribute("id"));
  }
 } 
}

So here as we have specified the var keyword with elements variable so automatically during the runtime the type of this ele variable will be <IWebElement> foreach ( var ele in elements ) so Console.WriteLine and in order to get the Id, we are going to use a method called GetAttribute().

So ("Id: {0}",ele.GetAttribute()); as we can see here we have just specified the ele variable with the var keyword and here the return type of this variable is automatically of type IWebElement and the reason behind it is because we are iterating the ele variable against the list of IWebElement.

So it has automatically changed the data type of this element to IWebElement and here we need to specify the attribute so let’s say ("id"); again we will build the solution and run the script in debug mode.

Output: It is going to locate all the web elements with the XPath as //input and will try to extract all the Id values of all those web elements.

When it is done if we look at the console output so we can see here it is able to extract the values of the Id attribute of all the web elements with this XPath.

So in this manner, we can use the FindElements method to locate all the web elements inside the webpage and we have used the XPath for locating, we can use other location strategies for locating the elements such as Id, Name, etc.