How to Find Elements with Selenium WebDriver 3?

The following tutorial is a complete guide for Creating a basic Selenium test automation framework with C#, Selenium WebDriver 3, Page Object Pattern and support for multiple browsers and data driven. In the last tutorial, we learned how to inspect elements. Now, let’s learn how find elements with Selenium WebDriver 3, in order to interact with them.


Tutorial Overview:

Class 1 – Creating an Automated Test Using Selenium WebDriver 3 and C#

Class 2 – How to Create a Test Automation Framework Architecture with Selenium WebDriver 3

Class 3 – Utilizing Test Automation Framework with Advanced Capabilities


We’ll be opening our test we created in Visual Studio in the first tutorial:

[TestFixture]
public class AutomationCore
{
    IWebDriver driver;

    [TestFixtureSetUp]
    public void startTest()
    {
        driver = new ChromeDriver();
        driver.Url = "http://blog.testproject.io/";
    }

    [TestFixtureTearDown]
    public void endtest()
    {
        driver.Quit();
    }

    [Test]
    public void blogTest()
    {
        Assert.IsTrue(Pages.contactUs.isAt());
    }
}

Finding elements on a web page before interacting with it, can be done on the WebDriver Instance(driver) itself or on a WebElement. WebDriver gives us the Find Element and Find Elements methods to find elements on the web page.

In order to reach the element, first we’ll find it through Selenium with the function FindElement which receives the method we’ll choose to find the element with.

Our example will be focusing on the field: ‘Your Name’. Considering we are willing to perform a certain action on this element, the element will be found via Chrome as we learned in the previous tutorial: Copy the value of the attribute – ‘Name’.

The final command:

driver.FindElement(By.Name(“your-name”))

After finding the element with Selenium, we are able to perform various actions on it. In our case, we’ll be sending text into the field. Thus, we’ll choose the function: SendKeys , as shown in the example below:

[TestFixture]
public class AutomationCore
{
    IWebDriver driver;

    [TestFixtureSetUp]
    public void startTest()
    {
        driver = new ChromeDriver();
        driver.Url = "http://blog.testproject.io/";
    }

    [TestFixtureTearDown]
    public void endtest()
    {
        driver.Quit();
    }

    [Test]
    public void blogTest()
    {
        Assert.IsTrue(Pages.contactUs.isAt());
        driver.FindElement(By.Name("your-name")).SendKeys("Test");
    }
}

FindElement is the most basic way to find elements, although not very convenient for code maintenance. Assuming the fact that there are hundreds of tests all using the same Selector and in case  one day, the Selector would change, we’ll have to go over each line of code and change it. To solve this issue, the recommendation is to build the test automation framework by implementing the Page Object Model, discussed in this tutorial.


Continue to the next step in creating a test automation frameworkHow to Create a Cross Browser Compatible Test Automation Framework.

-I’m curious to see what you think, please leave your questions and thoughts!