How To Use driver.FindElement And driver.FindElements In Selenium C#
Tags: selenium, webdriver, cloudtesting, automationtesting
Published: 2023-02-21 06:41:00

One of the essential parts when performing [automated UI testing](https://www.lambdatest.com/automated-ui-testing?utm_source=devto&utm_medium=organic&utm_campaign=feb20ap&utm_term=ap&utm_content=webpage), whether using [Selenium](https://www.lambdatest.com/selenium?utm_source=devto&utm_medium=organic&utm_campaign=feb20ap&utm_term=ap&utm_content=webpage) or another framework, is identifying the correct web elements the tests will interact with. However, if the web elements are not located correctly, you might get [NoSuchElementException in Selenium.](https://www.lambdatest.com/blog/49-common-selenium-exceptions-automation-testing/?utm_source=devto&utm_medium=organic&utm_campaign=feb20ap&utm_term=ap&utm_content=blog) This would cause a false negative result because we won’t get to the actual functionality check. Instead, our test will fail simply because it failed to interact with the correct element.

To do this, we have two methods: *driver.FindElement* and *driver.FindElements* in Selenium C#. The first one is needed when we want to identify a single element, such as an input field where we want to enter some text or a button on which we want to click. The second one is useful when we need to check multiple elements of the same type — for example, when we want to check that a drop-down list contains all the expected values.

In this blog on using *driver.FindElement* and *driver.FindElements* in Selenium C#, I will use Selenium 4.4.0. But, before we deep dive into how and when to use each of the *FindElement* and *FindElements* commands, let’s discuss how to identify the web elements on the page.

When we use the FindElement() and FindElements() commands, we need to identify the web elements by locators. The syntax is as follows:

```plaintext
1 driver.FindElement(By.locator_strategy)
```

Locators enable testers to select an HTML DOM element to interact with. Selecting the right [locator strategies](https://www.lambdatest.com/learning-hub/selenium-locators?utm_source=devto&utm_medium=organic&utm_campaign=feb20ap&utm_term=ap&utm_content=learning_hub) for these web elements can be crucial for the stability of the UI tests. You don’t want your tests to fail just because they didn’t find the correct button to click on or the correct text input to write in, right?

The locators are available for selection, and I will explain each of them below:

![](https://cdn-images-1.medium.com/max/2800/0*QdugCqF2MXUE20Pn.png align="left")

***This tutorial dive deep into*** [***web testing***](https://www.lambdatest.com/learning-hub/web-testing?utm_source=devto&utm_medium=organic&utm_campaign=feb20ap&utm_term=ap&utm_content=learning_hub) ***to help you understand its life cycle, elements, angles, the role of automation, and more.***

## Traditional Locators in Selenium

Selenium supports multiple locator strategies. Firstly, we have the traditional locators. These are locators that relate directly to the web element’s attributes. Traditional locators are supported in all versions of Selenium. The eight traditional locators are explained below:

### ID locator in Selenium

In a perfect world, each web element on our applications under test has an ID attribute. To find if the element has an ID or to see its ID, you simply have to right-click the element on the page and select Inspect (below I will demonstrate using the Chrome browser, but all major browsers have this functionality):

![](https://cdn-images-1.medium.com/max/2800/0*veDH6YRQjgRvIyOr.png align="left")

This will open up the Elements pane in the Developer Tools, which contains the page’s HTML code. Here, you can see the attributes of the HTML element. In my example, the field has an ID so I could use it in my UI tests.

***Perform manual or automated cross browser*** [***web testing***](https://www.lambdatest.com/?utm_source=devto&utm_medium=organic&utm_campaign=feb20ap&utm_term=ap&utm_content=webpage) ***on 3000+ browsers online. Deploy and scale faster with the most powerful cross browser testing tool online.***

![](https://cdn-images-1.medium.com/max/2800/0*4Mb84SGkdRADaDCE.png align="left")

So what I need to use in my tests is this:

```plaintext
1 driver.FindElement(By.Id("user-message"));
```

To learn more about it, you can refer to our earlier blog on [ID locator in Selenium](https://www.lambdatest.com/blog/making-the-move-with-id-locator-in-selenium-webdriver/?utm_source=devto&utm_medium=organic&utm_campaign=feb20ap&utm_term=ap&utm_content=blog).

### ClassName locator in Selenium

The class attribute in HTML is commonly used to point to a class in the CSS sheet. Multiple elements can share the same class and therefore the same styling.

The same element can also have multiple classes. You can see the class name (if the element has one) in the same panel as above:

![](https://cdn-images-1.medium.com/max/2800/0*H4g22S6WkOsQ-z4X.png align="left")

If the element has multiple classes, they are all displayed and separated by a space character. You can select one of the classes to use in the tests, which will translate to

```plaintext
1 driver.FindElement(By.ClassName("w-full"));
```

To learn more about it, you can refer to our earlier blog on the [ClassName locator in Selenium.](https://www.lambdatest.com/blog/selenium-java-tutorial-class-name-locator-in-selenium/?utm_source=devto&utm_medium=organic&utm_campaign=feb20ap&utm_term=ap&utm_content=blog)

### Name locator in Selenium

This is the Name attribute of the web element. The same as the ID and the ClassName, which you can find in the Elements panel of the Developer Tools.

![](https://cdn-images-1.medium.com/max/2800/0*7FgPKp-kALIb76Sf.png align="left")

This means:

```plaintext
driver.FindElement(By.Name("SearchDualList"));
```

To learn more about it, you can refer to our earlier blog on the [Name locator in Selenium](https://www.lambdatest.com/blog/how-to-use-name-locator-in-selenium-automation-scripts/?utm_source=devto&utm_medium=organic&utm_campaign=feb20ap&utm_term=ap&utm_content=blog).

### Tag Name locator in Selenium

This is the actual HTML tag of the element. So it will be a button, input, label, select, etc.

![](https://cdn-images-1.medium.com/max/2800/0*ywKqsktLQuI4nyyZ.png align="left")

```plaintext
driver.FindElement(By.TagName(“Input”));
```

To learn more about it, you can refer to our earlier blog on the [Name locator in Selenium.](https://www.lambdatest.com/blog/how-to-use-name-locator-in-selenium-automation-scripts/?utm_source=devto&utm_medium=organic&utm_campaign=feb20ap&utm_term=ap&utm_content=blog)

### Link Text locator in Selenium

This applies to link elements (the “a” tag in HTML) and refers to the visible link text.

***It’s crucial to debug websites for Safari before pushing them live. In this article, we look at how to debug websites using*** [***dev tools in Safari***](https://www.lambdatest.com/blog/debug-websites-using-safari-developer-tools/#none?utm_source=devto&utm_medium=organic&utm_campaign=feb20ap&utm_term=ap&utm_content=blog) ***Developer .***

![](https://cdn-images-1.medium.com/max/2800/0*2XkOOm0xK5VNDeDn.png align="left")

So to use this in your test, you will need:

```plaintext
driver.FindElement(By.LinkText("Simple Form Demo"));
```

To learn more about it, you can refer to our earlier blog on the [Link Text locator in Selenium.](https://www.lambdatest.com/blog/using-link-text-and-partial-link-text-in-selenium/?utm_source=devto&utm_medium=organic&utm_campaign=feb20ap&utm_term=ap&utm_content=blog)

### Partial Link Text locator in Selenium

The same as above, but using only a part of the text link. For the example above, I can have something like:

```plaintext
driver.FindElement(By.PartialLinkText("Form Demo"));
```

To learn more about it, you can refer to our earlier blog on the [Partial Link Text locator in Selenium.](https://www.lambdatest.com/blog/using-link-text-and-partial-link-text-in-selenium/?utm_source=devto&utm_medium=organic&utm_campaign=feb20ap&utm_term=ap&utm_content=blog)

### CSS Selector in Selenium

I left CSS Selectors and XPath last because, although they are widely used, they are not as straightforward as the other locators. The [CSS Selector in Selenium](https://www.lambdatest.com/blog/css-selectors-cheat-sheet/?utm_source=devto&utm_medium=organic&utm_campaign=feb20ap&utm_term=ap&utm_content=blog) is a combination of an element’s CSS Selectors and the selector’s value, used to identify the element on the web page. The basic syntax for CSS Selectors is *node\[attribute = ‘attribute\_value’\]*. For our example above, we would have:

```plaintext
driver.FindElement(By.CssSelector("input[id=’user_message’]"));
```

The big advantage of using CSS Selectors is that they allow us to combine multiple attributes to identify a single element, which is very useful when our elements don’t have an ID, a Name, or a ClassName, or when these attributes are not unique. It also allows us to identify elements relative to their position on the page (for example, the child input element of a span element).

### XPath in Selenium

Another very common locator strategy is [XPath in Selenium](https://www.lambdatest.com/blog/complete-guide-for-using-xpath-in-selenium-with-examples/?utm_source=devto&utm_medium=organic&utm_campaign=feb20ap&utm_term=ap&utm_content=blog). XPath is a language used for querying XML documents. It works on both HTML and XML documents, allowing us to navigate through the XML structure of any document. It contains a path expression and some conditions. The basic XPath syntax is *//tag\[@attribute=’value’\]*. For example:

```plaintext
driver.FindElement(By.XPath("//input[@id=’user-message’]"));
```

It’s important to note that when deciding on a locator strategy, some are faster, and some are slower. The ID locator strategy is the fastest and usually more recommended, while XPath tends to be slower.

## Relative Locators in Selenium

Relative locators have been introduced with [Selenium 4](https://www.lambdatest.com/learning-hub/selenium-4?utm_source=devto&utm_medium=organic&utm_campaign=feb20ap&utm_term=ap&utm_content=learning_hub), and they can be used to locate elements on the web page in relation to another element on that page. To learn more about it, you can go through our earlier log on [Relative Locators in Selenium 4.](https://www.lambdatest.com/blog/selenium-4-relative-locator/?utm_source=devto&utm_medium=organic&utm_campaign=feb20ap&utm_term=ap&utm_content=blog)

{% youtube FwFDA0822T0 %}


***This article explains the*** [***emulator vs simulator***](https://www.lambdatest.com/blog/emulator-vs-simulator-vs-real-device/?utm_source=devto&utm_medium=organic&utm_campaign=feb20ap&utm_term=ap&utm_content=blog) ***vs real device differences, the learning of which can help you select the right mobile testing solution for your business.***

The available relative locators are:

### Above

*Above* identifies an element that is located above a different element.

```plaintext
RelativeBy.WithLocator(By.TagName("input")).Above(By.Id("showInput"))
```

### Below

*Below* identifies an element that is located below a different element.

```plaintext
RelativeBy.WithLocator(By.TagName("input")).Below(By.TagName("li"))
```

### LeftOf

You probably already figured out how this goes. Yes, the element is located based on an element that can be found to its left.

```plaintext
RelativeBy.WithLocator(By.TagName("input")).LeftOf(By.TagName("p"))
```

### RightOf

Similar to the previous locator, it will retrieve the element based on an element to its right.

```plaintext
RelativeBy.WithLocator(By.TagName("input")).RightOf(By.TagName("p"))
```

### Near

*Near* will locate an element relative to an element that is within 50px.

```plaintext
RelativeBy.WithLocator(By.TagName("input")).Near(By.TagName("p"))
```

## How to find a single web element using driver.FindElement in Selenium C#?

Finding unique elements to interact with is the first common situation when writing automated tests with Selenium. For these cases, you will want to use the command *driver.FindElement* in Selenium C#. This will look something along these lines:

```plaintext
driver.FindElement(By.Id("user-message"));
```

This will return a web element. Then, you can perform any actions available for web elements in Selenium C#, such as SendKeys() to enter text, Click() to click on it, Clear() to clear its text, and so on.

To learn more about SendKeys, you can go through our earlier blog on [SendKeys in Selenium.](https://www.lambdatest.com/blog/how-to-use-selenium-sendkeys/?utm_source=devto&utm_medium=organic&utm_campaign=feb20ap&utm_term=ap&utm_content=blog)

Let’s look at an example.

**Test Scenario:**

I will exemplify this with a simple login test case. Here are the test steps:

1. Navigate to the webpage https://ecommerce-playground.lambdatest.io/index.php?route=account/login.
    
2. Enter valid credentials (EMail and Password).
    
3. Press the Login button.
    
4. Validate that the user account page is loaded.
    

For all the web elements interaction stated, we are looking to identify a single element to perform actions on:

1. Insert text into the E-Mail Address input.
    
2. Insert text into the Password input.
    
3. Click the Login button.
    
4. Find the MyAccount header on the page.
    

In this [Selenium C# tutorial](https://www.lambdatest.com/learning-hub/selenium-c-sharp-tutorial?utm_source=devto&utm_medium=organic&utm_campaign=feb20ap&utm_term=ap&utm_content=learning_hub) on using driver.FindElement and driver.FindElements in Selenium C#, I’ll be running my tests on the LambdaTest cloud grid using different OS and browser configurations.

Cloud testing platforms like LambdaTest allow you to perform manual and automated [cross browser testing](https://www.lambdatest.com/?utm_source=devto&utm_medium=organic&utm_campaign=feb20ap&utm_term=ap&utm_content=webpage) on an [online browser farm](https://www.lambdatest.com/online-browser-farm?utm_source=devto&utm_medium=organic&utm_campaign=feb20ap&utm_term=ap&utm_content=webpage) of 3000+ browsers and browser versions at scale. It provides support for different [automation testing frameworks](https://www.lambdatest.com/blog/best-test-automation-frameworks-2021/?utm_source=v&utm_medium=organic&utm_campaign=feb20ap&utm_term=ap&utm_content=blog) like Selenium, [Cypress](https://www.lambdatest.com/learning-hub/cypress-tutorial?utm_source=devto&utm_medium=organic&utm_campaign=feb20ap&utm_term=ap&utm_content=learning_hub), [Playwright](http://www.lambdatest.com/playwright?utm_source=devto&utm_medium=organic&utm_campaign=feb20ap&utm_term=ap&utm_content=webpage), and more. Furthermore, using parallel [test execution](https://www.lambdatest.com/learning-hub/test-execution?utm_source=devto&utm_medium=organic&utm_campaign=feb20ap&utm_term=ap&utm_content=learning_hub), you can also trim down lead time by multiple folds.

{% youtube jrgx_3gfWVA %}


Subscribe to the [LambdaTest YouTube Channel](https://www.youtube.com/c/LambdaTest?sub_confirmation=1?utm_source=devto&utm_medium=organic&utm_campaign=feb20ap&utm_term=ap&utm_content=youtube) to see our latest tutorials about [Selenium automation](https://www.lambdatest.com/selenium-automation?utm_source=devto&utm_medium=organic&utm_campaign=feb20ap&utm_term=ap&utm_content=webpage), [Appium automation](https://www.lambdatest.com/appium?utm_source=devto&utm_medium=organic&utm_campaign=feb20ap&utm_term=ap&utm_content=webpage), [Playwright automation,](https://www.lambdatest.com/blog/playwright-framework?utm_source=devto&utm_medium=organic&utm_campaign=feb20ap&utm_term=ap&utm_content=webpage) and more.

So, let’s get started!

***Need a great solution for testing of*** [***Safari browser on windows***](https://www.lambdatest.com/safari-browser-for-windows?utm_source=devto&utm_medium=organic&utm_campaign=feb20ap&utm_term=ap&utm_content=webpage?) ***Forget about emulators or simulators — use real online browsers. Try LambdaTest for free.***

## Project Setup

The first step of the implementation is setting up the project in Visual Studio. I’ll be using [NUnit](https://www.lambdatest.com/learning-hub/nunit-tutorial?utm_source=devto&utm_medium=organic&utm_campaign=feb20ap&utm_term=ap&utm_content=learning_hub) as my test framework, but if you have other preferences, like xUnit or MSTest, they will do the trick just as well. You can see a detailed comparison between [NUnit vs. XUnit vs. MSTest](https://www.lambdatest.com/blog/nunit-vs-xunit-vs-mstest/?utm_source=devto&utm_medium=organic&utm_campaign=feb20ap&utm_term=ap&utm_content=learning_hub) to choose the right framework for your testing needs.

For this, select the project type you need in Visual Studio:

![](https://cdn-images-1.medium.com/max/2000/0*KcgHlYxmNi3DKykG.png align="left")

![](https://cdn-images-1.medium.com/max/2306/0*WDbjyJD8Yudelrsv.png align="left")

Enter a name for the project, select the location, and you should end up with a default created test class that looks like this

```plaintext
using NUnit.Framework; namespace SeleniumLocators { public class Tests { [SetUp] public void Setup() { } [Test] public void Test1() { Assert.Pass(); } } } 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 using NUnit.Framework; namespace SeleniumLocators { public class Tests { [SetUp] public void Setup() { } [Test] public void Test1() { Assert.Pass(); } } }
```

Next, we need to add the NuGet packages for Selenium. You can do this by right-clicking on the project name in the solution explorer and selecting Manage NuGet packages:

![](https://cdn-images-1.medium.com/max/2000/0*BWIjTQLxKa8JuGur.png align="left")

This will open up the package manager. In the browse tab, look for each of the following packages and click install for each of them:

* Selenium.WebDriver
    
* [Selenium.Support](http://Selenium.Support)
    
* DotNetSeleniumExtras.WaitHelpers
    

Once installed, they will be visible in the Installed tab, along with the installed version:

![](https://cdn-images-1.medium.com/max/2800/0*m87iRrJ9i7KdeEMO.png align="left")

I am not testing locally. I want to do my tests on the latest version of Safari (currently 16.0). Since I’m a Windows user, the LambdaTest Grid is helpful because it allows me to test a configuration I cannot set up on my machine. I am using the [Automation Capabilities Generator](https://www.lambdatest.com/capabilities-generator/?utm_source=devto&utm_medium=organic&utm_campaign=feb20ap&utm_term=ap&utm_content=webpage) to generate the desired configuration.

![](https://cdn-images-1.medium.com/max/2800/0*LZ_ccD2JRARisL7a.png align="left")

## Implementation

Now that everything’s set up, here’s the implementation for our test scenario:

```plaintext
using NUnit.Framework; using OpenQA.Selenium; using OpenQA.Selenium.Remote; using OpenQA.Selenium.Safari; using OpenQA.Selenium.Support.UI; using SeleniumExtras.WaitHelpers;
```

```plaintext
namespace SeleniumLocators { public class Tests { private static IWebDriver driver; private static readonly string gridURL = "@hub.lambdatest.com/wd/hub"; private static readonly string LT_USERNAME = Environment.GetEnvironmentVariable("LT_USERNAME"); private static readonly string LT_ACCESS_KEY = Environment.GetEnvironmentVariable("LT_ACCESS_KEY"); private readonly string loginURL = "https://ecommerce-playground.lambdatest.io/index.php?route=account/login"; private readonly string homePageURL = "https://ecommerce-playground.lambdatest.io/index.php";
```

```plaintext
[SetUp] public void Setup() { DriverOptions capabilities = new SafariOptions(); capabilities.BrowserVersion = "16.0"; Dictionary<string, object> ltOptions = new Dictionary<string, object>(); ltOptions.Add("username", LT_USERNAME); ltOptions.Add("accessKey", LT_ACCESS_KEY); ltOptions.Add("platformName", "MacOS Ventura"); ltOptions.Add("build", "[DEMO] E-commerce"); ltOptions.Add("w3c", true); ltOptions.Add("project", "LambdaTest Playground"); ltOptions.Add("plugin", "c#-c#"); capabilities.AddAdditionalOption("LT:Options", ltOptions); driver = new RemoteWebDriver(new Uri($"https://{LT_USERNAME}:{LT_ACCESS_KEY}{gridURL}"), capabilities); }
```

```plaintext
[Test] public void ValidateSignIn() { driver.Navigate().GoToUrl("https://ecommerce-playground.lambdatest.io/index.php?route=account/login"); driver.FindElement(By.Name("email")).SendKeys("andreea@getnada.com"); driver.FindElement(By.Id("input-password")).SendKeys("test"); driver.FindElement(By.CssSelector("input[value='Login']")).Click(); WebDriverWait wait = new WebDriverWait(driver, TimeSpan.FromSeconds(5)); wait.Until(ExpectedConditions.ElementExists(By.XPath("//h2[text()='My Account']"))); } } }
```

## Code Walkthrough

Let’s walk through the code now and see what’s going on.

The first lines of code, the **using** ones, are there to instruct our test class on what packages to use:

![](https://cdn-images-1.medium.com/max/2000/0*7XsXa4komauVNa4C.png align="left")

Next, we have the class, which contains our actual test. In the first part, I declared the variables I’ll need in my tests:

![](https://cdn-images-1.medium.com/max/2800/0*md1i9CyY6bmjS_g6.png align="left")

I need an IWebDriver instance to interact with the browser. And the gridURL, LT\_USERNAME, and LT\_ACCESS\_KEY are string variables that I will be using in the setup to connect to my LambdaTest Grid account. I also stored the login page URL as a variable to make it easier to read. It’s also a good idea to store credentials (such as the LT\_USERNAME and LT\_ACCESS\_KEY) as environment variables in your operating system.

![](https://cdn-images-1.medium.com/max/2800/0*507OPXWgP4pLL1yp.png align="left")

The *\[SetUp\]* is an *NUnit* attribute, marking a method that will run before each test method. Consider this method as the precondition of the tests. Here I set up my driver and my LambdaTest configuration.

Then all this goes into the *SetUp* method and a new instance of the driver, which is linked to my account URL.

Next comes the actual test, where you can see the *FindElement* in action. Again, we have an NUnit attribute, which is \[Test\]. This marks a test method. If you open the Test Explorer of Visual Studio (from the Test menu → Test Explorer), you will see all the methods with this attribute. I currently have just one:

![](https://cdn-images-1.medium.com/max/2000/0*QcPfnzOaksvA64cQ.png align="left")

![](https://cdn-images-1.medium.com/max/2800/0*xtvFNyIMLYwZW-jS.png align="left")

The first line is the navigate command, which instructs the browser to go to the desired URL. In our case, the Login page.

Next, we have various interactions with web elements on the page. I tried to use as many of the available locators, so as you can see, I’ve used: *driver.FindElement(*[*By.Name*](http://By.Name)*(“email”))* to locate the Email input field:

![](https://cdn-images-1.medium.com/max/2800/0*9NRbsPWssYM4UH24.png align="left")

*driver.FindElement(*[*By.Id*](http://By.Id)*(“input-password”))* to locate the Password field:

![](https://cdn-images-1.medium.com/max/2800/0*43fkQPonoVrktatT.png align="left")

And driver.FindElement(By.CssSelector(“input\[value=’Login’\]”))using a CSS Selector build on the value attribute, to locate the Login button:

![](https://cdn-images-1.medium.com/max/2800/0*MvHAWGVMGDXtRbgq.png align="left")

Next, once the elements are correctly located, simply use Selenium’s methods to interact with them:

1. SendKeys() to enter text in the input fields (for email and password)
    
2. Click() to press the login button.
    

You can learn more about the Action Class through this blog on [Action Class in Selenium](https://www.lambdatest.com/blog/what-is-actions-class-in-selenium/?utm_source=devto&utm_medium=organic&utm_campaign=feb20ap&utm_term=ap&utm_content=blog). The following lines instruct the browser to wait for 5 seconds or until the element with the given XPath exists, using an explicit wait. The test will fail if the element is not found within 5 seconds.

Now my test is displayed in Visual Studio’s Test Explorer, and I can run it from here by right-clicking on it and selecting Run or by pressing the Run button at the top:

![](https://cdn-images-1.medium.com/max/2136/0*lXPHRXBCj8h8UH5W.png align="left")

Once the test finishes running, I can see the result in the same panel (green means passed)

![](https://cdn-images-1.medium.com/max/2124/0*6cLC4s_81s3iVIPl.png align="left")

As well as in my Dashboard in LambdaTest [Selenium Grid](https://www.lambdatest.com/blog/selenium-grid-setup-tutorial/?utm_source=devto&utm_medium=organic&utm_campaign=feb20ap&utm_term=ap&utm_content=blog) in the Automation tab:

![](https://cdn-images-1.medium.com/max/2800/0*CieRo9J6wjDRNEeN.png align="left")

From here, I can go deeper into details and see a recording of my test, as well as each detailed step:

![](https://cdn-images-1.medium.com/max/2800/0*B_rimlWG2YFx9F57.png align="left")

[LambdaTest Analytics](https://www.lambdatest.com/test-analytics?utm_source=devto&utm_medium=organic&utm_campaign=feb20ap&utm_term=ap&utm_content=webpage) is a comprehensive solution that helps you manage and optimize your QA processes and test data through access to powerful analytics capabilities.

With LambdaTest Analytics, you can easily access, analyze, and streamline your QA efforts, ensuring that your product is successful by incorporating key analytics insights. Whether you are looking to improve your testing efficiency or simply want to better understand the performance of your product, LambdaTest Analytics is what you need.

![](https://cdn-images-1.medium.com/max/2400/0*f1D8MvdpdKPCwY3J.png align="left")

***Perform manual or automated cross*** [***browser testing***](https://www.lambdatest.com/?utm_source=devto&utm_medium=organic&utm_campaign=feb20ap&utm_term=ap&utm_content=webpage) ***on 3000+ browsers online. Deploy and scale faster with the most powerful cross browser testing tool online.***

## How to find multiple web elements using driver.FindElements in Selenium C#?

The second case I’ll present is when you are not looking for a single element but trying to find all the elements that meet certain criteria. The Selenium C# method used for this is FindElements(), and it works pretty much the same way as FindElement(), except that this method returns a collection of web elements:

```plaintext
driver.FindElements(By.XPath("//a[text()='HTC Touch HD']"));
```

It’s better to illustrate this with an example as well.

**Test Scenario:**

1. Navigate to the ecommerce-playground.lambdatest.io/index.php?route=common/home page.
    
2. Search for “HTC” products.
    
3. Validate the required number of products returned (in this case, 8).
    

![](https://cdn-images-1.medium.com/max/2800/0*I3IT6Z9yAfHUK5sk.png align="left")

## Implementation

```plaintext
[Test] public void ValidateNumberOfProducts() { int expectedNumber = 8; driver.Navigate().GoToUrl("https://ecommerce-playground.lambdatest.io/index.php?route=common/home"); driver.FindElement(By.Name("search")).SendKeys("htc"); driver.FindElement(By.XPath("//button[text()='Search']")).Click(); WebDriverWait wait = new WebDriverWait(driver, TimeSpan.FromSeconds(5)); wait.Until(ExpectedConditions.ElementExists(By.XPath("//h1[text()='Search - htc']"))); var results = driver.FindElements(By.XPath("//a[text()='HTC Touch HD']")); Assert.That(results, Has.Count.EqualTo(expectedNumber)); }
```

## Code Walkthrough

I also added a new string variable for the URL:

![](https://cdn-images-1.medium.com/max/2800/0*XdpSnXBoo1rFNIKx.png align="left")

Next, inside the test, I created a simple integer variable that stores the expected number of results, which I will use later:

![](https://cdn-images-1.medium.com/max/2000/0*xSDxCN4zbzE7p7yQ.png align="left")

This test should be easy to understand after we already went over the first one. The second line is the same, except we navigate to a different page:

![](https://cdn-images-1.medium.com/max/2000/0*742w7Rub8LzgBPwI.png align="left")

Next, using the Selenium driver.FindElement() method, I have two interactions with elements:

![](https://cdn-images-1.medium.com/max/2624/0*9GHB3XBFXqz6OITa.png align="left")

I used a wait again to ensure that I validated after loading the correct page. I’m using the H1 web element with the text “Search — htc” to be displayed:

![](https://cdn-images-1.medium.com/max/2800/0*aiYJD1dUciH0bj8w.png align="left")

![](https://cdn-images-1.medium.com/max/2800/0*Tf4Skni1FWxFJ3u8.png align="left")

And then we have the *driver.FindElements()* in Selenium C#, where we identify all the elements that have the same locator, in this case, the same XPath:

![](https://cdn-images-1.medium.com/max/2800/0*qKcIkLHYJqfMTQSw.png align="left")

The collection of elements is saved within a variable, which I used in the last line. Here I verify the number of web elements returned by the driver.FindElements() in Selenium C# is the number I expect:

![](https://cdn-images-1.medium.com/max/2316/0*EDotHWMQlid_cTgk.png align="left")

Assert is a class provided by NUnit, which allows us to make validations. If the condition we pass as a parameter is true, then the test passes. Otherwise, it will fail.

Now that the tests are written, you can see them both in the Test Explorer. To run them both, you just need to press the Run All button, right-click and select Run, or use the Ctrl+R, T shortcut, just like before:

![](https://cdn-images-1.medium.com/max/2000/0*1tH1CvvEst_yzfr3.png align="left")

After the tests are finished running, you’ll be able to see the results in Visual Studio:

![](https://cdn-images-1.medium.com/max/2000/0*-bs4bEkY_3YBZ92c.png align="left")

And now, you can see the new test execution in your LambdaTest account, under the Automation tab:

![](https://cdn-images-1.medium.com/max/2800/0*nCtKcEM5shgcVRXL.png align="left")

And, if you click on one of the runs, you can see a video recording of the test, the configuration on which the test ran

![](https://cdn-images-1.medium.com/max/2000/0*AnTZWzH1RvNAFMnb.png align="left")

And a step-by-step description of the Selenium commands sent:

![](https://cdn-images-1.medium.com/max/2138/0*IzZim9xH8cJi1NTz.png align="left")

If you are a tester seeking to improve your abilities, consider obtaining the Selenium C# 101 certification from LambdaTest as a way to demonstrate your expertise. This certification can help you to stand out and highlight your skills to potential employers or clients.

## Final Thoughts

The *driver.FindElement()* and *driver.FindElements()* in *Selenium C#* are two of the most common commands when working on web UI automation. Here are the most important things to remember:

1. The syntax in Selenium C# for finding elements is:
    
    ```plaintext
    driver.FindElement(By.locator_strategy); driver.FindElements(By.locator_strategy);
    ```
    
2. *driver.FindElement()* in Selenium C# returns the first web element found using the provided locator. If you need to select a specific element, ensure that the locator strategy will return only one element.
    
3. *driver.FindElements()* in Selenium C# returns a collection of web elements.
    
4. It’s crucial to use a good locator strategy; otherwise, you can end up with unwanted exceptions, or Selenium might try to interact with the incorrect element on the web page.