Selenium 中的右键单击和双击(示例)

Selenium 中的右键单击

Selenium WebDriver 中的右键单击操作可以使用 Actions 类完成。右键单击操作在 Selenium 中也称为上下文单击 (Context Click)。Actions 类提供的预定义方法 `context click` 用于执行右键单击操作。以下是演示使用 Actions 类进行右键单击操作的代码。

Actions actions = new Actions(driver);
WebElement elementLocator = driver.findElement(By.id("ID"));
actions.contextClick(elementLocator).perform();

如何在 Selenium 中右键单击

测试场景

  1. 启动 URL:https://demo.guru99.com/test/simple_context_menu.html
  2. 在按钮上执行右键单击操作:“right click me”
  3. 单击显示的右键单击选项列表中的“编辑”链接
  4. 单击显示的警报上的“确定”按钮
  5. 关闭浏览器

代码

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();

}
}

结果

Right Click in Selenium

Selenium 中的双击

Selenium WebDriver 中的双击操作可以使用 Actions 类完成。Actions 类是 Selenium WebDriver 中预定义的类,用于执行多种键盘和鼠标操作,例如右键单击、拖放等。

使用 Actions 类在 Selenium 中双击

Actions actions = new Actions(driver);
WebElement elementLocator = driver.findElement(By.id("ID"));
actions.doubleClick(elementLocator).perform();
  • 首先,我们需要通过传递驱动程序实例作为参数来实例化 Actions 类的对象。
  • 使用 find element 命令,我们需要找到要双击的元素的定位符。
  • 使用 Actions 类的预定义双击方法,我们需要对网页元素执行双击操作。

如何在 Selenium 中双击

测试场景

代码

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();

}
}

结果

点击“Double-Click Me to See Alert”按钮,显示弹出窗口。

Double Click in Selenium

在 Eclipse 中,您可以在控制台中看到输出。

Double Click in Selenium

摘要

  • Selenium 中的 Actions 类主要用于执行复杂的键盘和鼠标操作。因此,与 JavaScript 相比,Actions 类更受青睐,用于在 Selenium 中执行右键单击和双击等操作。
  • 右键单击操作主要用于在对元素进行右键单击时打开新菜单。Selenium WebDriver 中的右键单击操作可以使用下面提到的预定义命令 Context Click 完成。
    Actions action = new Actions(driver);
    WebElement link = driver.findElement(By.ID ("Element ID"));
    action.contextClick(link).perform();
    
  • 双击操作用于在双击操作后网页元素的状态发生变化时。Selenium WebDriver 中的双击操作可以使用下面提到的预定义命令 Double Click 完成。
    Actions action = new Actions(driver);
    WebElement link = driver.findElement(By.ID ("Element ID"));
    action. doubleClick (link).perform();