- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
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(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.
Syntax
Actions a = new Actions(driver); a.MoveByOffset(10,20).Perform(); a.Click().Perform() //move to an element IWebElement l = driver.FindElement(By.name("txtnam")); a.MoveToElement(l).Perform();
Let us try to move the mouse to the Library link and then click it.
Example
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 = "https://www.tutorialspoint.com/index.htm"; IWebDriver driver; [SetUp] public void Setup(){ //creating object of FirefoxDriver driver = new FirefoxDriver(""); } [Test] public void Test2(){ //URL launch driver.Navigate().GoToUrl(url); //identify element IWebElement l = driver.FindElement(By.XPath("//*[text()='Library']")); //object of Actions class Actions a = new Actions(driver); //move to element a.MoveToElement(l); //click a.Click(); a.Perform(); Console.WriteLine("Page title: " + driver.Title); } [TearDown] public void close_Browser(){ driver.Quit(); } } }
Output
- Related Articles
- Execute a script as long as the mouse pointer is moving over an element in HTML?
- How to hide or disable the mouse pointer in Tkinter?
- Execute a script when a mouse pointer moves over an element in HTML?
- Execute a script when a mouse pointer moves out of an element in HTML?
- How to perform releasing a mouse on an element in Selenium with python?
- How to perform mouse movement to an element in Selenium with python?
- How to change the mouse pointer color in Tkinter?
- How to capture the screenshot of a specific element rather than entire page using Selenium Webdriver?
- Downloading a file at a specified location through python and selenium using Chrome driver.
- Downloading file to specified location with Selenium and python.
- How to find specific lines in a table using Selenium?
- How to download any file and save it to the desired location using Selenium Webdriver?
- How to use selenium to check if element contains specific class attribute?
- How to scroll a specific DIV using Selenium WebDriver with Java?
- How to get the attribute value of a web element in Selenium (using Java or Python)?

Advertisements