• 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.tw/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,表示程式碼已成功執行。

示例 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.tw/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.tw/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 相關的其他內容,以加深您的理解並拓寬您的視野。

廣告
© . All rights reserved.