Selenium C# Tutorial: User Interactions API for mouse and keyboard events(C#)
Updated: Mar 5
The Selenium WebDriver's advanced user interactions API allows us to perform basic operations such as simulating keyboard and mouse events to complex event scenarios (e.g., holding a key and then performing mouse operations, dragging-and-dropping, etc.).

To use these advanced methods, we will need to add to our code an additional namespace (OpenQA.Selenium.Interactions ) that provides us the "Actions" class. The Actions class implements the builder pattern to create a composite action containing a
group of other actions.
Prior we dive into the code examples. here is a summary of the main methods related to this class:

Simulating Double-Click on an element
As part of a testing project, there will be a time that there will be elements that we have to test using double-click events for triggering some actions. The following code will simulate a "Double-Click" event on a button. As a result, the text located in the first field will be copied to the second one.
Code:
class Program
{
static void Main(string[] args)
{
//Preperations:
string siteToAccess = "file:///C:/Users/home pc3/Desktop/checkbox%20Example.html";
IWebDriver chromedriver = new ChromeDriver();
chromedriver.Navigate().GoToUrl(siteToAccess);
IWebElement button;
//Method of the Actions class is called
Actions trigger = new Actions(chromedriver);
//Locating the buttong element
button = chromedriver.FindElement(By.XPath("/html/body/button"));
//Call the doubleClick() method by passing the element
trigger.MoveToElement(button).DoubleClick().Build().Perform();
}
Simulating a single mouse click on an element
The same logic was used in the previous example, but now we simulate a 'single' mouse click instead of a "Double-Click."

string siteToAccess = "https://www.agilequalitymadeeasy.com/";
IWebDriver chromedriver = new ChromeDriver();
chromedriver.Navigate().GoToUrl(siteToAccess);
IWebElement button;
//Method of the Actions class is called
Actions trigger = new Actions(chromedriver);
//Locating the buttong element
button = chromedriver.FindElement(By.Id("comp-khbyvzuo2label"));
//Call the Click() method by passing the element
trigger.MoveToElement(button).Click().Build().Perform();
Performing drag-and-drop operations
We will use the actions class to perform the "drag-and-drop" operations in this recipe. To drag an element onto another element and drop it, we first need to locate the 'Source' and 'Target' elements and pass them as input to the dragAndDrop() method of the Actions class.
string siteToAccess = "https://javascript.info/article/mouse-drag-and-drop/ball4/";
IWebDriver chromedriver = new ChromeDriver();
chromedriver.Navigate().GoToUrl(siteToAccess);
//Method of the Actions class is called
Actions trigger = new Actions(chromedriver);
//source and target elements where the source element will be dragged - and - dropped.
IWebElement source = chromedriver.FindElement(By.Id("ball"));
IWebElement target = chromedriver.FindElement(By.Id("gate"));
//Option 1:
trigger.DragAndDrop(source, target).Perform();
//Option 2: trigger.ClickAndHold(source).MoveToElement(target).Release(target).Build().Perform();

Simulating Mouse and Keyboard events
Selenium API supports Keystroke combinations of mouse and keyboard events; the following code example will simulate a situation where the user selects multiple values with the "Ctrl" key and Left mouse button clicks:
HTMl:
<select name="AnimalNames" size="5" multiple="multiple" style="width:100px">
<option value="fo">Fox</option>
<option value="du">Duck</option>
<option value="sh">Shark</option>
<option value="ti">Tigar</option>
<option value="ea">Eagle</option>
</select>
Code:
//Preperations:
string siteToAccess = "file:///C:/Users/home-pc3/Desktop/checkbox%20Example.html";
IWebDriver chromedriver = new ChromeDriver();
chromedriver.Navigate().GoToUrl(siteToAccess);
//Method of the Actions class is called
Actions MultipleSelectionUsingCTRL = new Actions(chromedriver);
//Get all items on the list
IList<IWebElement> ListOfElements = chromedriver.FindElements(By.XPath("//option"));
//Selecting specifc items MultipleSelectionUsingCTRL.Click(ListOfElements[2]).KeyDown(Keys.Control).Click(ListOfElements[3]).Click(ListOfElements[4]).KeyDown(Keys.Control).KeyDown(Keys.Control).Build().Perform();
Result:
