使用C#和Selenium将鼠标指针移动到特定位置或元素
我们可以使用Actions类在Selenium webdriver (C#)中将鼠标指针移动到特定位置或元素。我们首先需要创建此类的对象。
接下来,要移动元素,我们需要应用MoveToElement方法并将元素定位器作为参数传递给此方法。最后,要实际执行此任务,需要使用Perform方法。
移动到元素后,我们可以使用Click方法单击它。要移动到特定位置,我们必须使用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();让我们尝试将鼠标移动到“图书馆”链接,然后单击它。

示例
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://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();
}
}
}输出

广告
数据结构
网络
关系数据库管理系统 (RDBMS)
操作系统
Java
iOS
HTML
CSS
Android
Python
C语言编程
C++
C#
MongoDB
MySQL
Javascript
PHP