Nhấp chuột phải và Double Nhấp vào Selenium (Ví dụ)
Nhấp chuột phải vào Selenium
Hành động nhấp chuột phải vào Selenium trình điều khiển web có thể được thực hiện bằng lớp Hành động. Thao tác Click chuột phải còn gọi là Context Click in Selenium. Nhấp chuột theo ngữ cảnh của phương thức được xác định trước do lớp Hành động cung cấp được sử dụng để thực hiện thao tác nhấp chuột phải. Dưới đây là mã minh họa thao tác nhấp chuột phải bằng lớp Hành động.
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
Kịch bản thử nghiệm:
- Khởi chạy URL: https://demo.guru99.com/test/simple_context_menu.html
- Thực hiện thao tác Right Click vào nút: right click me
- Nhấn vào liên kết Edit trên danh sách hiển thị các tùy chọn nhấn chuột phải
- Nhấp vào nút OK trên cảnh báo được hiển thị
- Đó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(); //Launch the Application Under Test (AUT) driver.get("https://demo.guru99.com/test/simple_context_menu.html"); driver.manage().window().maximize(); // Right click the button to launch right click menu options Actions action = new Actions(driver); WebElement link = driver.findElement(By.cssSelector(".context-menu-one")); action.contextClick(link).perform(); // Click on Edit link on the displayed menu options WebElement element = driver.findElement(By.cssSelector(".context-menu-icon-copy")); element.click(); // Accept the alert displayed //driver.switchTo().alert().accept(); // Closing the driver instance //driver.quit(); } }
Kết quả:
Double nhấp vào Selenium
Double bấm vào hành động trong Selenium trình điều khiển web có thể được thực hiện bằng lớp Hành động. Lớp hành động là lớp được xác định trước trong Selenium trình điều khiển web được sử dụng để thực hiện nhiều thao tác bàn phím và chuột như Nhấp chuột phải, Kéo và Thả, v.v.
Double nhấp vào Selenium sử dụng lớp Hành động
Actions actions = new Actions(driver); WebElement elementLocator = driver.findElement(By.id("ID")); actions.doubleClick(elementLocator).perform();
- Ban đầu, chúng ta cần khởi tạo một đối tượng của lớp Actions bằng cách truyền phiên bản trình điều khiển làm tham số
- Sử dụng lệnh tìm phần tử, chúng ta cần tìm vị trí của phần tử mà chúng ta muốn nhấp đúp
- Sử dụng phương thức nhấp đúp được xác định trước của lớp Actions, chúng ta cần thực hiện thao tác nhấp đúp vào phần tử web
Làm thế nào để Double Nhấp vào Selenium
Kịch bản thử nghiệm
- Khởi chạy URL: https://demo.guru99.com/test/simple_context_menu.html
- Double nhấp vào nút có nhãn 'Double-Click vào tôi để xem cảnh báo'
- Nhấp vào nút OK trên cảnh báo được hiển thị
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 DobuleClickDemo { public static void main(String[] args) throws InterruptedException { WebDriver driver; System.setProperty("webdriver.chrome.driver","X://chromedriver.exe"); driver= new ChromeDriver(); //Launch the Application Under Test (AUT) driver.get("https://demo.guru99.com/test/simple_context_menu.html"); driver.manage().window().maximize(); driver.get("https://demo.guru99.com/test/simple_context_menu.html"); driver.manage().window().maximize(); //Double click the button to launch an alertbox Actions action = new Actions(driver); WebElement link =driver.findElement(By.xpath("//button[text()='Double-Click Me To See Alert']")); action.doubleClick(link).perform(); //Switch to the alert box and click on OK button Alert alert = driver.switchTo().alert(); System.out.println("Alert Text\n" +alert.getText()); alert.accept(); //Closing the driver instance //driver.quit(); } }
Kết quả:
Nút có nhãn “Double-Click Me to See Alert” được nhấp vào và cửa sổ bật lên được hiển thị
In Eclipse, bạn sẽ thấy kết quả trong bảng điều khiển
Tổng kết
- Lớp hành động trong Selenium chủ yếu được sử dụng để thực hiện các thao tác phức tạp trên bàn phím và chuột. Do đó, lớp Actions được ưa chuộng hơn so với Javascript để thực hiện các thao tác như Nhấp chuột phải và Double Nhấp vào Selenium.
- Thao tác nhấp chuột phải chủ yếu được sử dụng khi thực hiện nhấp chuột phải vào một phần tử sẽ mở ra một menu mới. Thao tác nhấp chuột phải vào Selenium trình điều khiển web có thể được thực hiện bằng lệnh được xác định trước Context Click như được đề cập bên dưới
Actions action = new Actions(driver); WebElement link = driver.findElement(By.ID ("Element ID")); action.contextClick(link).perform();
- Double Thao tác nhấp chuột được sử dụng khi trạng thái của phần tử web thay đổi sau thao tác nhấp đúp. Double Bấm vào thao tác trong Selenium trình điều khiển web có thể được thực hiện bằng lệnh được xác định trước Double Bấm vào như được đề cập dưới đây
Actions action = new Actions(driver); WebElement link = driver.findElement(By.ID ("Element ID")); action. doubleClick (link).perform();