เราสามารถย้ายตัวชี้เมาส์ไปยังตำแหน่งหรือองค์ประกอบเฉพาะใน Selenium webdriver(C#) โดยใช้คลาส Actions เราต้องสร้างวัตถุของคลาสนี้ก่อน
ถัดจากการย้ายองค์ประกอบ เราต้องใช้เมธอด MoveToElement และส่งตัวระบุตำแหน่งองค์ประกอบเป็นพารามิเตอร์สำหรับวิธีนี้ สุดท้ายนี้ เพื่อใช้งานจริง ต้องใช้วิธี Perform
หลังจากย้ายไปยังองค์ประกอบ เราสามารถคลิกที่มันด้วยวิธีคลิก ในการย้ายไปยังตำแหน่งเฉพาะ เราต้องใช้เมธอด MoveByOffset แล้วส่งตัวเลขออฟเซ็ตที่จะเลื่อนไปตามแกน x และ y เป็นพารามิเตอร์ไป
ไวยากรณ์
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(); ให้เราลองเลื่อนเมาส์ไปที่ลิงค์ Library แล้วคลิกเลย

ตัวอย่าง
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();
}
}
} ผลลัพธ์
