- Selenium 教程
- Selenium - 首頁
- Selenium - 概述
- Selenium - 元件
- Selenium - 自動化測試
- Selenium - 環境搭建
- Selenium - 遠端控制
- Selenium IDE 教程
- Selenium - IDE 介紹
- Selenium - 特性
- Selenium - 限制
- Selenium - 安裝
- Selenium - 建立測試
- Selenium - 建立指令碼
- Selenium - 控制流
- Selenium - 儲存變數
- Selenium - 警報和彈出視窗
- Selenium - Selenese 命令
- Selenium - Actions 命令
- Selenium - Accessors 命令
- Selenium - Assertions 命令
- Selenium - Assert/Verify 方法
- Selenium - 定位策略
- Selenium - 指令碼除錯
- Selenium - 驗證點
- Selenium - 模式匹配
- Selenium - JSON 資料檔案
- Selenium - 瀏覽器執行
- Selenium - 使用者擴充套件
- Selenium - 程式碼匯出
- Selenium - 程式碼輸出
- Selenium - JavaScript 函式
- Selenium - 外掛
- Selenium WebDriver 教程
- Selenium - 介紹
- Selenium WebDriver vs RC
- Selenium - 安裝
- Selenium - 第一個測試指令碼
- Selenium - 驅動程式會話
- Selenium - 瀏覽器選項
- Selenium - Chrome 選項
- Selenium - Edge 選項
- Selenium - Firefox 選項
- Selenium - Safari 選項
- Selenium - 雙擊
- Selenium - 右鍵單擊
- Python 中的 HTML 報告
- 處理編輯框
- Selenium - 單個元素
- Selenium - 多個元素
- Selenium Web 元素
- Selenium - 檔案上傳
- Selenium - 定位器策略
- Selenium - 相對定位器
- Selenium - 查詢器
- Selenium - 查詢所有連結
- Selenium - 使用者互動
- Selenium - WebElement 命令
- Selenium - 瀏覽器互動
- Selenium - 瀏覽器命令
- Selenium - 瀏覽器導航
- Selenium - 警報和彈出視窗
- Selenium - 處理表單
- Selenium - 視窗和標籤頁
- Selenium - 處理連結
- Selenium - 輸入框
- Selenium - 單選按鈕
- Selenium - 複選框
- Selenium - 下拉框
- Selenium - 處理 iframe
- Selenium - 處理 Cookie
- Selenium - 日期時間選擇器
- Selenium - 動態 Web 表格
- Selenium - Actions 類
- Selenium - Action 類
- Selenium - 鍵盤事件
- Selenium - 鍵上/下
- Selenium - 複製和貼上
- Selenium - 處理特殊鍵
- Selenium - 滑鼠事件
- Selenium - 拖放
- Selenium - 筆事件
- Selenium - 滾動操作
- Selenium - 等待策略
- Selenium - 顯式/隱式等待
- Selenium - 支援特性
- Selenium - 多選
- Selenium - 等待支援
- Selenium - 選擇支援
- Selenium - 顏色支援
- Selenium - ThreadGuard
- Selenium - 錯誤和日誌記錄
- Selenium - 異常處理
- Selenium - 其他
- Selenium - 處理 Ajax 呼叫
- Selenium - JSON 資料檔案
- Selenium - CSV 資料檔案
- Selenium - Excel 資料檔案
- Selenium - 跨瀏覽器測試
- Selenium - 多瀏覽器測試
- Selenium - 多視窗測試
- Selenium - JavaScript 執行器
- Selenium - 無頭執行
- Selenium - 擷取螢幕截圖
- Selenium - 錄製影片
- Selenium - 頁面物件模型
- Selenium - 頁面工廠
- Selenium - 錄製和回放
- Selenium - 框架
- Selenium - 瀏覽上下文
- Selenium - DevTools
- Selenium Grid 教程
- Selenium - 概述
- Selenium - 架構
- Selenium - 元件
- Selenium - 配置
- Selenium - 建立測試指令碼
- Selenium - 測試執行
- Selenium - 端點
- Selenium - 自定義節點
- Selenium 報告工具
- Selenium - 報告工具
- Selenium - TestNG
- Selenium - JUnit
- Selenium - Allure
- Selenium & 其他技術
- Selenium - Java 教程
- Selenium - Python 教程
- Selenium - C# 教程
- Selenium - Javascript 教程
- Selenium - Kotlin 教程
- Selenium - Ruby 教程
- Selenium - Maven & Jenkins
- Selenium - 資料庫測試
- Selenium - LogExpert 日誌記錄
- Selenium - Log4j 日誌記錄
- Selenium - Robot Framework
- Selenium - AutoIT
- Selenium - Flash 測試
- Selenium - Apache Ant
- Selenium - Github 教程
- Selenium - SoapUI
- Selenium - Cucumber
- Selenium - IntelliJ
- Selenium - XPath
- Selenium 其他概念
- Selenium - IE 驅動程式
- Selenium - 自動化框架
- Selenium - 關鍵字驅動框架
- Selenium - 資料驅動框架
- Selenium - 混合驅動框架
- Selenium - SSL 證書錯誤
- Selenium - 替代方案
- Selenium 有用資源
- Selenium - 問答
- Selenium - 快速指南
- Selenium - 有用資源
- Selenium - 自動化實踐
- Selenium - 討論
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 - 單擊、右鍵單擊、雙擊
讓我們以以下頁面為例,我們將在其中分別透過單擊網頁上的單擊我、右鍵單擊我和雙擊我按鈕來執行單擊、右鍵單擊和雙擊操作。
分別執行單擊我、右鍵單擊我和雙擊我按鈕後,我們將分別在頁面上獲得訊息您進行了一次動態點選和您進行了雙擊。
程式碼實現
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最初為黑色。
但是,懸停並按住它時,選單 Navbar 的顏色會發生變化,如下面的影像所示。
程式碼實現
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 - 單擊元素的選單和子選單
讓我們來看另一個例子,我們將單擊選單下拉選單。單擊後,將顯示所有子選單 - 操作、另一個操作和其他內容。
程式碼實現
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 相關的其他內容,以加深您的理解並拓寬您的視野。
