logo logo

How to Find Elements with Selenium WebDriver 3?

main post image

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 to 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!


 

Asya Galinsky

About the author

Asya Galinsky

Join TestProject Community

Get full access to the world's first cloud-based, open source friendly testing community. Enjoy TestProject's end-to-end test automation Platform, Forum, Blog and Docs - All for FREE.

Join Us Now  

Comments

9 4 comments
  • Avatar
    hitend January 22, 2018, 7:59 pm

    Thanks. Page Object Model “this tutorial” link gives 404 – page not found. please fix it.

    Hiten

  • Avatar
    Magoo February 4, 2019, 12:01 pm

    Hi,

    Managed to muddle through errors on previous tutorials, but on this one I’m getting:

    Error CS0229 Ambiguity between ‘AutomationCore.driver’ and ‘AutomationCore.driver’ NUnitTestProject1

    What am I missing here?

    • Avatar
      Magoo February 4, 2019, 12:12 pm

      Note, this currently what my program looks like:

      using OpenQA.Selenium;
      using OpenQA.Selenium.Chrome;
      using NUnit.Framework;
      namespace Test
      {
      [TestFixture]
      public class AutomationCore
      {
      IWebDriver driver;

      [SetUp]
      public void startTest()
      {
      driver = new ChromeDriver(@”C:\Users\Magoo\Downloads”);
      driver.Url = “http://blog.testproject.io/”;
      }
      [TearDown]
      public void endtest()
      {
      driver.Quit();
      }
      [Test]
      public void blogTest()
      {
      Assert.IsTrue(driver.Title.Contains(“Blog”));
      driver.FindElement(By.Name(“your-name”)).SendKeys(“Test”);
      }
      }
      }

      Had to change the Assert.IsTrue line as it wouldn’t recognise the Pages thing from a previous test, and had to include the file location for chromedriver I was getting an error saying it couldn’t find the file unless I specified the file location.

Leave a Reply

popup image

Selenium for Teams

Sharing and distributing Selenium tests has never been so easy! With TestProject's FREE Selenium based platform, you can finally create awesome tests with the freedom to collaborate with your team effortlessly.
Sign Up Now right arrow
FacebookLinkedInTwitterEmail