• Selenium Video Tutorials

Selenium WebDriver - 鼠标事件



Selenium Webdriver 可用于执行鼠标事件操作,例如使用 Actions 类执行右键单击、左键单击、双击、鼠标悬停和移动。contextClick()、doubleClick()、click() 和 moveToElement() 方法用于执行这些操作。

Actions 类中鼠标事件的基本方法

Actions 类中有多种方法可以执行鼠标事件。

  • click() − 此方法用于在鼠标当前位置执行单击。

  • build() − 此方法用于创建一个包含所有要执行的操作的动作组合。

  • perform() − 此方法用于在不首先调用 build() 的情况下执行操作。

  • release() − 此方法用于在鼠标当前位置释放鼠标操作。

  • release(WebElement e) − 此方法用于在作为参数传递的 webElement e 的中间释放鼠标操作。

  • click(WebElement e) − 此方法用于在作为参数传递的 webElement e 的中间执行单击。

  • doubleClick() − 此方法用于在鼠标当前位置执行双击。

  • doubleClick(WebElement e) − 此方法用于在作为参数传递的 webElement e 的中间执行双击。

  • contextClick() − 此方法用于在鼠标当前位置执行右键单击。

  • contextClick(WebElement e) − 此方法用于在作为参数传递的 webElement e 的中间执行右键单击。

  • moveToElement(WebElement e) − 此方法用于将鼠标移动到作为参数传递的 webElement e 的中间。

  • moveToElement(WebElement e, int x-offset, int y-offset) − 此方法用于将鼠标移动到视点中元素的偏移位置。webelement e、x 和 y 偏移值作为参数传递。

  • clickAndHold() − 此方法用于在鼠标当前位置执行单击(不释放)。

  • clickAndHold(WebElement e) − 此方法用于在作为参数传递的 webElement e 的中间执行单击(不释放)。

示例 1 - 单击、右键单击、双击

让我们以以下页面为例,我们将在其中分别通过单击网页上的单击我、右键单击我双击我按钮来执行单击、右键单击和双击操作。

Selenium Mouse Events 1

分别执行单击我、右键单击我双击我按钮后,我们将分别在页面上获得消息您进行了一次动态点击您进行了双击

Selenium Mouse Events 2

代码实现

package org.example; import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.chrome.ChromeDriver; import org.openqa.selenium.interactions.Actions; import java.util.concurrent.TimeUnit; public class ActionsMouse { public static void main(String[] args) throws InterruptedException { // Initiate the Webdriver WebDriver driver = new ChromeDriver(); // adding implicit wait of 15 secs driver.manage().timeouts().implicitlyWait(15, TimeUnit.SECONDS); // Opening the webpage driver.get("https://tutorialspoint.com/selenium/practice/buttons.php"); // identify element with xpath for click WebElement m = driver.findElement (By.xpath("/html/body/main/div/div/div[2]/button[1]")); // object of Actions class to move then click Actions a = new Actions(driver); a.moveToElement(m).click().build().perform(); // get text after click WebElement t = driver.findElement(By.xpath("//*[@id='welcomeDiv']")); System.out.println("Text after click: " + t.getText()); // identify element with xpath for double click WebElement n = driver.findElement (By.xpath("/html/body/main/div/div/div[2]/button[3]")); // double click a.moveToElement(n).doubleClick().build().perform(); // get text after double click WebElement x = driver.findElement(By.xpath("//*[@id='doublec']")); System.out.println("Text after double click: " + x.getText()); // identify element with xpath for right click WebElement y = driver.findElement (By.xpath("/html/body/main/div/div/div[2]/button[2]")); // right click a.moveToElement(y).contextClick().build().perform(); // Closing browser driver.quit(); } }

输出

Text after click: You have done a dynamic click
Text after double click: You have Double clicked

Process finished with exit code 0

在上面的示例中,我们执行了单击、双击和右键单击,然后在控制台中收到消息 - 单击后的文本:您进行了一次动态点击双击后的文本:您进行了双击

最后,收到消息进程已完成,退出代码为 0,表示代码已成功执行。

Explore our latest online courses and learn new skills at your own pace. Enroll and become a certified expert to boost your career.

示例 2 - 将鼠标悬停在元素上

让我们来看另一个例子,其中菜单Navbar最初为黑色。

Selenium Mouse Events 3

但是,悬停并按住它时,菜单 Navbar 的颜色会发生变化,如下面的图像所示。

Selenium Mouse Events 4

代码实现

package org.example; import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.chrome.ChromeDriver; import org.openqa.selenium.interactions.Actions; import java.util.concurrent.TimeUnit; public class ActionsClickandHold { public static void main(String[] args) throws InterruptedException { // Initiate the Webdriver WebDriver driver = new ChromeDriver(); // adding implicit wait of 15 secs driver.manage().timeouts().implicitlyWait(15, TimeUnit.SECONDS); // Opening the webpage driver.get("https://tutorialspoint.com/selenium/practice/menu.php#"); // identify element with xpath for click and hold WebElement m = driver.findElement (By.xpath("/html/body/main/div/div/div[2]/nav/div/a")); // get element color in rgba format String s = m.getCssValue("color"); System.out.println("rgba code for color element: " + s ); // object of Actions class to click and hold Actions a = new Actions(driver); a.clickAndHold(m).build().perform(); // get element color in rgba format String c = m.getCssValue("color"); System.out.println("rgba code for color for element after click and hold: " + c); // Closing browser driver.quit(); } }

输出

rgba code for color element: rgba(51, 51, 51, 1)
rgba code for color for element after click and hold: rgba(64, 169, 68, 1)

Process finished with exit code 0

示例 3 - 单击元素的菜单和子菜单

让我们来看另一个例子,我们将单击菜单下拉菜单。单击后,将显示所有子菜单 - 操作、另一个操作其他内容

Selenium Mouse Events 5

代码实现

package org.example; import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.chrome.ChromeDriver; import org.openqa.selenium.interactions.Actions; import java.util.List; import java.util.concurrent.TimeUnit; public class ActionsMenu { public static void main(String[] args) throws InterruptedException { // Initiate the Webdriver WebDriver driver = new ChromeDriver(); // adding implicit wait of 15 secs driver.manage().timeouts().implicitlyWait(15, TimeUnit.SECONDS); // Opening the webpage driver.get("https://tutorialspoint.com/selenium/practice/menu.php#"); // identify element with xpath for menu WebElement m = driver.findElement (By.xpath("//*[@id='navbarSupportedContent']/ul/li[3]/a")); // object of Actions class to click and hold Actions a = new Actions(driver); a.moveToElement(m).click().build().perform(); // identify all sub-menu under main menu List<WebElement> l= driver.findElements (By.xpath("//*[@id='navbarSupportedContent']/ul/li[3]/ul/li")); for(int i = 0; i < l.size(); i++){ System.out.println("Sub-menus are: " + l.get(i).getText()); } // Closing browser driver.quit(); } }

输出

Sub-menus are: Action
Sub-menus are: Another action
Sub-menus are: 
Sub-menus are: Something else here

Process finished with exit code 0

在上面的示例中,我们单击了一个菜单,之后我们得到了所有子菜单,并在控制台中显示消息 - 子菜单是:操作,子菜单是:另一个操作,子菜单是:子菜单是:此处还有其他内容

最后,收到消息进程已完成,退出代码为 0,表示代码已成功执行。

本教程到此结束,本教程全面介绍了 Selenium Webdriver - 鼠标事件。我们首先介绍了 Actions 类中鼠标事件的基本方法,并通过示例说明了如何在 Selenium Webdriver 中处理鼠标事件。这使您能够深入了解 Selenium Webdriver - 鼠标事件。明智的做法是继续练习您所学的内容,并探索与 Selenium 相关的其他内容,以加深您的理解并拓宽您的视野。

广告