Handle Dropdown Mouse Actions and Windows Using Selenium C#

Introduction

In this article, we will understand how to handle dropdown mouse action methods and windows using Selenium with C#.

Prerequisite

In Html, the select tag is used to create the dropdown

Select tags can contain one or more <option> tags as their children, each representing an item in the dropdown. The <option> tags define the individual options that users can choose from in the dropdown.

In order to handle dropdowns, we need to make use of select classes in Selenium.

Select a class from this package.

using OpenQA.Selenium.Support.UI;

Create a select class object.

Pass the reference of the webelement to the select class.

Now use the select class object and call the below functions.

  • selectByVisibleText
  • selectByIndex
  • selectByValue

selectByVisibleText

This method will match with the visible text attribute so that you can click the dropdown value for which it becomes matched.

selectByValue

we need to pass a text as a parameter to select_by_value. Then, it will match with the value attribute text so that the user can click the dropdown value for which it becomes matched.

selectByIndex

Index values start from 0 and continue with an increment of +1. It means if there are five dropdown values, then they will be 0,1,2,3 and 4. Suppose you want to select the 3rd value from the dropdown, then the index will be 2.

Code with explanation

using OpenQA.Selenium;
using OpenQA.Selenium.Chrome;
using OpenQA.Selenium.Support.UI;

class Program
{
    static void Main()
    {
        // Set the path to the ChromeDriver executable
        string driverPath = "path\\to\\chromedriver.exe";

        // Initialize the Chrome driver
        IWebDriver driver = new ChromeDriver(driverPath);

        // Navigate to the webpage with the dropdown
        driver.Navigate().GoToUrl("https://example.com");

        // Identify the dropdown element by its ID, name, XPath, or other locators
        IWebElement dropdown = driver.FindElement(By.Id("dropdownId"));

        // Use SelectElement to work with the dropdown
        SelectElement select = new SelectElement(dropdown);

        // Select by text
        select.SelectByText("Option 1");

        // or select by value
        select.SelectByValue("option1Value");

        // or select by index
        select.SelectByIndex(0);

        // Perform other actions or assertions as needed

        // Close the browser
        driver.Quit();
    }
}

Handling Windows

Web applications often open new windows or pop-ups, presenting a challenge for us. Selenium provides methods to switch between different browser windows.

The browser window often called the main or parent window, represents the homepage or the currently open web page a user sees when opening a browser. When a Selenium automation script runs, it typically starts with the parent window.

Selenium WebDriver session involves opening a window that is initially controlled by the WebDriver.

When we click on a button or URL link in the parent window, and the action opens another window(s) within the main window, the new window(s) is called a child window.

we can handle multiple browser windows using the WindowHandles property of the IWebDriver interface.

Code with explanation

using OpenQA.Selenium;
using OpenQA.Selenium.Chrome;
using System;
using System.Collections.Generic;

class Program
{
    static void Main()
    {
        // Set the path to the ChromeDriver executable
        string driverPath = "path\\to\\chromedriver.exe";

        // Initialize the Chrome driver
        IWebDriver driver = new ChromeDriver(driverPath);

        // Navigate to the first webpage
        driver.Navigate().GoToUrl("application url which u are testing");

        // Open a new window (e.g., by clicking a link that opens in a new window)
        IWebElement linkToNewWindow = driver.FindElement(By.LinkText("Open New Window"));
        linkToNewWindow.Click();

        // Get the handles of all open windows
        IReadOnlyCollection<string> windowHandles = driver.WindowHandles;

        // Switch to the new window
        foreach (string windowHandle in windowHandles)
        {
            if (windowHandle != driver.CurrentWindowHandle)
            {
                driver.SwitchTo().Window(windowHandle);
                break;
            }
        }

        // Now you are working in the new window
        // Perform actions or assertions as needed

        // Close the new window
        driver.Close();

        // Switch back to the original window
        driver.SwitchTo().Window(windowHandles.First());

        // Perform actions or assertions in the original window

        // Close the original window or quit the driver
        driver.Quit();
    }
}

Mouse Actions

Click

we can use the Click() method provided by the IWebElement interface in order to click the element.

// Find an element by its ID, name, XPath, or other locators
        IWebElement element = driver.FindElement(By.Id("yourElementId"));

// Click on the element
        element.Click();

Double Click

we can perform a double-click on an element using the Actions class.

// Find an element by its ID, name, XPath, or other locators
        IWebElement element = driver.FindElement(By.Id("yourElementId"));

// Create an Actions object
        Actions actions = new Actions(driver);

// Perform a double-click on the element
        actions.DoubleClick(element).Perform();

Drag and Drop

we can perform a drag and drop on an element using the Actions class.

/ Find the draggable element
        IWebElement draggable = driver.FindElement(By.Id("draggable"));

// Find the droppable element
        IWebElement droppable = driver.FindElement(By.Id("droppable"));

// Create an Actions object
        Actions actions = new Actions(driver);

 // Perform drag-and-drop
        actions.DragAndDrop(draggable, droppable).Perform();

Summary

I hope this article will be helpful in handling dropdowns and windows and learning to perform several mouse actions using selenium with C#.

Thanks, Happy learning.


Recommended Free Ebook
Similar Articles