Selenium C# — Identify DOM Element

Tarun Babbar
2 min readMay 26, 2017

--

Hey Guys,
Before I go in deep to tell you various techniques to identify DOM elements on a web page, I recommend you to please visit this link to set up initial Visual Studio test automation solution. This link will give you idea on how to execute tests across browsers.

OK, so now you have your initial test automation set up done, lets move ahead and see various ways through which you can identify DOM elements from a web application.

Modes Available:

Selenium web driver has exposed several methods which will help you identify you DOM element. Remember, how we identified google search textbox and button in this?

I simply created an instance of InternetExplorer webDriver and called FindElement method. Inside FindElement method, selenium has provided you several ways to search your DOM element. Below are the important which is mostly used:

  1. By.ClassName
  2. By.CssSelector
  3. By.Id
  4. By.LinkText
  5. By.Name
  6. By.PartialLinkText
  7. By.TagName
  8. By.XPath

You can find single element or multiple elements using the above search.
You can use the below code if single element matches the search criteria.

In case, you are searching for multiple elements for a search criteria, example google search resulting in pagination at the bottom of the page as below (Google search resulting in pagination), then you search all these links as shown in Searching multiple elements on google search results with TagName.

Google search resulting in pagination

Please do look into the source code placed at github.

--

--