Nhấp chuột phải và Double Nhấp vào Selenium
⚡ Tóm tắt thông minh
Nhấp chuột phải và Double Nhấp vào Selenium Các thao tác chuột được tự động hóa thông qua lớp Actions. Hướng dẫn này minh họa cả hai thao tác với ví dụ hoạt động. Java mã nguồn, các kịch bản kiểm thử thực tế và các phương pháp thúc đẩy chúng. Selenium WebDriver.

Nhấp chuột phải vào Selenium
Hành động nhấp chuột phải vào Selenium WebDriver được thực hiện bằng cách sử dụng lớp Actions. Thao tác này cũng được gọi là Context Click. Các tham số được định nghĩa trước contextClick() Phương thức của lớp Actions thực hiện thao tác nhấp chuột phải. Dưới đây là cú pháp cơ bản.
Actions actions = new Actions(driver);
WebElement elementLocator = driver.findElement(By.id("ID"));
actions.contextClick(elementLocator).perform();
Cách nhấp chuột phải vào Selenium
Tình huống dưới đây khởi động một GuruTrang demo 99, thực hiện thao tác nhấp chuột phải và chọn một tùy chọn từ menu ngữ cảnh hiện ra.
Kịch bản thử nghiệm:
- Phóng: https://demo.guru99.com/test/simple_context_menu.html
- Nhấp chuột phải vào nút "Nhấp chuột phải vào tôi"
- Nhấp vào liên kết Chỉnh sửa trên menu hiển thị.
- Nhấp vào OK trên thông báo
- Đóng trình duyệt
Mã Code:
package test;
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;
public class ContextClick {
public static void main(String[] args) throws InterruptedException {
WebDriver driver;
System.setProperty("webdriver.chrome.driver", "X://chromedriver.exe");
driver = new ChromeDriver();
driver.get("https://demo.guru99.com/test/simple_context_menu.html");
driver.manage().window().maximize();
Actions action = new Actions(driver);
WebElement link = driver.findElement(By.cssSelector(".context-menu-one"));
action.contextClick(link).perform();
WebElement element = driver.findElement(By.cssSelector(".context-menu-icon-copy"));
element.click();
}
}
Kết quả: Menu ngữ cảnh hiện ra và tùy chọn Chỉnh sửa được chọn.
Double Nhấp vào Selenium
Theo cùng một mô hình dựa trên hành động, Double Nhấp vào Selenium WebDriver sử dụng các định nghĩa được xác định trước. doubleClick() Phương thức. Lớp Actions là lớp hỗ trợ tiêu chuẩn cho các thao tác kết hợp giữa chuột và bàn phím như Nhấp chuột phải, Kéo và Thả, và Di chuột.
Actions actions = new Actions(driver);
WebElement elementLocator = driver.findElement(By.id("ID"));
actions.doubleClick(elementLocator).perform();
Luồng thực thi như sau:
- Khởi tạo một đối tượng Actions bằng cách sử dụng thể hiện của trình điều khiển.
- Xác định vị trí phần tử mục tiêu bằng
findElement. - Gọi số
doubleClick()và chuỗiperform()để thực thi.
Làm thế nào để Double Nhấp vào Selenium
Tình huống tiếp theo minh họa thao tác nhấp đúp chuột hoàn toàn, kích hoạt một... JavaCảnh báo trong tập lệnh và xác nhận nó bằng lập trình.
Kịch bản thử nghiệm:
- Phóng: https://demo.guru99.com/test/simple_context_menu.html
- Double nhấp vào nút “Double-Bấm vào đây để xem thông báo”
- Nhấp vào OK trên thông báo
Mã Code:
package test;
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 org.openqa.selenium.Alert;
public class DoubleClickDemo {
public static void main(String[] args) throws InterruptedException {
WebDriver driver;
System.setProperty("webdriver.chrome.driver", "X://chromedriver.exe");
driver = new ChromeDriver();
driver.get("https://demo.guru99.com/test/simple_context_menu.html");
driver.manage().window().maximize();
Actions action = new Actions(driver);
WebElement link = driver.findElement(By.xpath("//button[text()='Double-Click Me To See Alert']"));
action.doubleClick(link).perform();
Alert alert = driver.switchTo().alert();
System.out.println("Alert Text\n" + alert.getText());
alert.accept();
}
}
Kết quả: Thông báo hiện lên và nội dung thông báo được in ra. Eclipse giao diện điều khiển.



