Moving mouse pointer to a specific location or element using C# and Selenium

SeleniumCsharpAutomation TestingTesting Tools

We can move mouse pointer to a specific location or element in Selenium webdriver(C#) using the Actions class. We have to first create an object of this class.

Next to move an element we have to apply the MoveToElement method and pass the element locator as a parameter to this method. Finally, to actually perform this task the method Perform is to be used.

After moving to an element, we can click on it with the Click method. To move to a specific location, we have to use the MoveByOffset method and then pass the offset numbers to be shifted along the x and y axis as parameters to it.


Actions a = new Actions(driver);
//move to an element
IWebElement l = driver.FindElement("txtnam"));

Let us try to move the mouse to the Library link and then click it.


using NUnit.Framework;
using OpenQA.Selenium;
using OpenQA.Selenium.Firefox;
using System;
using OpenQA.Selenium;
using OpenQA.Selenium.Interactions;
namespace NUnitTestProject2{
   public class Tests{
      String url = "";
      IWebDriver driver;
      public void Setup(){
         //creating object of FirefoxDriver
         driver = new FirefoxDriver("");
      public void Test2(){
         //URL launch
         //identify element
         IWebElement l = driver.FindElement(By.XPath("//*[text()='Library']"));
         //object of Actions class
         Actions a = new Actions(driver);
         //move to element
         Console.WriteLine("Page title: " + driver.Title);
      public void close_Browser(){


Published on 07-Apr-2021 08:38:14