右键单击并 Double 点击进入 Selenium

⚡ 智能摘要

右键单击并 Double 点击进入 Selenium 鼠标操作是通过 Actions 类实现的自动化操作。本教程演示了这两种操作的实际应用。 Java 代码、真实测试场景以及驱动它们的方法 Selenium WebDriver。

  • 🖱️ 核心方法: Actions 类公开了 contextClick()(用于右键单击)和 doubleClick()(用于双击任何 WebElement)。
  • 🧭 工作流程模式: 找到元素,使用驱动程序实例化 Actions,调用操作,并链接 perform() 来执行。
  • 🧪 测试场景: 这两个示例都以 demo.guru99.com/test/simple_context_menu.html 为目标,以获得已验证、可重复的结果。
  • ???? 行动区分: 右键单击打开上下文菜单;双击触发状态更改,例如发出警报或进行编辑。
  • 🤖 人工智能集成: 自愈定位器和人工智能辅助 Selenium 框架可以减少点击操作时的不稳定情况。

右键单击并 Double 点击进入 Selenium

右键单击 Selenium

右键单击操作 Selenium WebDriver 使用 Actions 类来实现操作。该操作也称为上下文点击。预定义 contextClick() Actions 类的该方法执行右键单击操作。以下是基本语法。

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

如何右键单击 Selenium

以下场景启动 Guru99 演示页面,执行右键单击,并从出现的上下文菜单中选择一个选项。

测试场景:

  1. 发射: https://demo.guru99.com/test/simple_context_menu.html
  2. 点击“右键点击我”按钮
  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();

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

结果: 此时会显示上下文菜单,并选择“编辑”选项。

右键单击 Selenium

Double 点击进入 Selenium

遵循同样的基于行动的模式, Double 点击进入 Selenium WebDriver 使用预定义的 doubleClick() 方法。Actions 类是用于复合鼠标和键盘操作(例如右键单击、拖放和悬停)的标准辅助类。

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

执行流程如下:

  • 使用驱动程序实例实例化 Actions 对象。
  • 使用以下方式定位目标元素 findElement.
  • 电话联系 doubleClick() 和链 perform() 执行。

如何 Double 点击进入 Selenium

下一个场景演示了如何通过完整的双击操作触发一个事件。 Java脚本发出警报并通过程序进行确认。

测试场景:

代码:

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

结果: 弹出提示框,提示文本打印出来。 Eclipse 安慰。

Double 点击进入 Selenium 警报

Double 点击控制台输出

常见问题

WebElement.click() 只会触发鼠标左键单击事件。右键单击和双击是复合鼠标事件,需要 Actions 类通过 perform() 函数进行链式调用和分发。

是的。contextClick() 模拟真实的右键单击操作,并打开应用程序的上下文菜单。这两个术语在应用程序中可以互换使用。 Selenium 文档。

Actions 类会对事件进行排队。如果没有 perform() 函数,链式调用的步骤将永远不会被执行。perform() 函数会清空队列,并按顺序执行每个排队的操作。

是的, Java脚本执行器可以触发双击事件,但它会绕过真正的浏览器事件。建议使用操作方法。 Selenium 标准。

是的。在无头Chrome浏览器中,通过“操作”功能右键单击和双击都可以正常工作。 Firefox 前提是支持 W3C Actions API。 Selenium 4 个驱动程序完全支持此功能。

行动类到达 Selenium WebDriver 2.0 位于 org.openqa.selenium.interactions 中,并保留在 Selenium 3 和 4 支持 W3C Actions。

人工智能工具和 Selenium 替代品 应用自愈定位器,使其能够适应点击过程中的元素变化,从而减少因动态 ID 或布局变化而导致的不稳定故障。

是的。人工智能代码助手可以将纯英文场景转换为 Selenium Java or Python 使用 contextClick() 和 doubleClick() 的代码,并建议使用强大的定位器和等待机制。

总结一下这篇文章: