Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Moving mouse pointer to a specific location or element using C# and Selenium
We can move mouse pointer to a specific location or element in Selenium WebDriver with C# using the Actions class. This class provides methods to simulate user interactions like moving the mouse, clicking, and performing complex gestures on web elements.
To move the mouse to an element, we use the MoveToElement method and pass the element locator as a parameter. To move to specific coordinates, we use the MoveByOffset method. All actions must be executed using the Perform method.
Syntax
Following is the syntax for creating an Actions object and moving to an element −
Actions actions = new Actions(driver); actions.MoveToElement(webElement).Perform();
Following is the syntax for moving to specific coordinates −
Actions actions = new Actions(driver); actions.MoveByOffset(x, y).Perform();
Following is the syntax for chaining multiple actions −
actions.MoveToElement(element).Click().Perform();
Using MoveToElement Method
The MoveToElement method moves the mouse pointer to the center of the specified web element. This is useful for hovering over elements or preparing for click actions −
using System;
using OpenQA.Selenium;
using OpenQA.Selenium.Chrome;
using OpenQA.Selenium.Interactions;
class Program {
static void Main() {
IWebDriver driver = new ChromeDriver();
try {
driver.Navigate().GoToUrl("https://www.tutorialspoint.com/index.htm");
// Find the Library link
IWebElement libraryLink = driver.FindElement(By.XPath("//*[text()='Library']"));
// Create Actions object
Actions actions = new Actions(driver);
// Move to the element and click
actions.MoveToElement(libraryLink).Click().Perform();
Console.WriteLine("Successfully moved to element and clicked");
Console.WriteLine("Page title: " + driver.Title);
}
finally {
driver.Quit();
}
}
}
The output of the above code is −
Successfully moved to element and clicked Page title: Free Online Tutorials and Courses
Using MoveByOffset Method
The MoveByOffset method moves the mouse by a given offset from its current position. This is useful for precise positioning −
using System;
using OpenQA.Selenium;
using OpenQA.Selenium.Chrome;
using OpenQA.Selenium.Interactions;
class Program {
static void Main() {
IWebDriver driver = new ChromeDriver();
try {
driver.Navigate().GoToUrl("https://www.tutorialspoint.com/index.htm");
Actions actions = new Actions(driver);
// Move by offset from current position
actions.MoveByOffset(100, 50).Perform();
// Perform a click at the new position
actions.Click().Perform();
Console.WriteLine("Mouse moved by offset (100, 50) and clicked");
}
finally {
driver.Quit();
}
}
}
The output of the above code is −
Mouse moved by offset (100, 50) and clicked
Combining Mouse Actions
You can chain multiple actions together before calling Perform to execute them as a single sequence −
using System;
using OpenQA.Selenium;
using OpenQA.Selenium.Chrome;
using OpenQA.Selenium.Interactions;
class Program {
static void Main() {
IWebDriver driver = new ChromeDriver();
try {
driver.Navigate().GoToUrl("https://www.tutorialspoint.com/index.htm");
// Find an element to hover over
IWebElement menuItem = driver.FindElement(By.XPath("//a[contains(text(), 'Tutorials')]"));
Actions actions = new Actions(driver);
// Chain multiple actions: move to element, pause, then click
actions.MoveToElement(menuItem)
.Pause(TimeSpan.FromSeconds(1))
.Click()
.Perform();
Console.WriteLine("Chained actions executed successfully");
Console.WriteLine("Current URL: " + driver.Url);
}
finally {
driver.Quit();
}
}
}
The output of the above code is −
Chained actions executed successfully Current URL: https://www.tutorialspoint.com/tutorialslibrary.htm
Common Use Cases
Hover effects: Moving mouse to elements to trigger CSS hover states or dropdown menus
Drag and drop: Moving to source element before dragging to target location
Tooltips: Hovering over elements to display tooltip information
Context menus: Right-clicking at specific locations to open context menus
Conclusion
The Actions class in Selenium WebDriver provides powerful mouse movement capabilities through MoveToElement and MoveByOffset methods. These methods enable precise control over mouse positioning for complex user interactions. Always remember to call Perform() to execute the actions.
