右键单击并 Double 点击进入 Selenium (例子)
右键点击进去 Selenium
右键单击操作 Selenium 可以使用 Actions 类来完成 web 驱动程序。右键单击操作也称为上下文单击 Selenium. Actions 类提供的预定义方法 context click 用于执行右键单击操作。以下是使用 Actions 类演示右键单击操作的代码。
Actions actions = new Actions(driver); WebElement elementLocator = driver.findElement(By.id("ID")); actions.contextClick(elementLocator).perform();
如何右键单击 Selenium
测试场景:
- 启动 URL: https://demo.guru99.com/test/simple_context_menu.html
- 在按钮上执行右键单击操作:右键单击我
- 在显示的右键单击选项列表中单击“编辑”链接
- 单击显示的警报上的“确定”按钮
- 关闭浏览器
代码:
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(); } }
结果:
Double 点击进入 Selenium
Double 点击操作 Selenium 可以使用 Actions 类来完成 web 驱动程序。Actions 类是 Selenium Web 驱动程序用于执行多种键盘和鼠标操作,例如右键单击、拖放等。
Double 点击进入 Selenium 使用 Actions 类
Actions actions = new Actions(driver); WebElement elementLocator = driver.findElement(By.id("ID")); actions.doubleClick(elementLocator).perform();
- 首先,我们需要通过将驱动程序实例作为参数传递来实例化 Actions 类的对象
- 使用查找元素命令,我们需要找到要双击的元素的定位器
- 使用Actions类预定义的双击方法,我们需要对Web元素执行双击操作
如何 Double 点击进入 Selenium
测试场景
- 启动 URL: https://demo.guru99.com/test/simple_context_menu.html
- Double 点击标有“Double-点击我查看警报'
- 单击显示的警报上的“确定”按钮
代码:
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-点击“点击我查看警报”并显示弹出窗口
In Eclipse,您会在控制台中看到输出
总结
- 动作类 Selenium 主要用于执行复杂的键盘和鼠标操作。因此,与 Javascript 用于执行右键单击等操作 Double 点击进入 Selenium.
- 右键单击操作最常用于在元素上单击鼠标右键以打开新菜单的情况。 Selenium 可以使用预定义命令上下文单击来完成 Web 驱动程序,如下所示
Actions action = new Actions(driver); WebElement link = driver.findElement(By.ID ("Element ID")); action.contextClick(link).perform();
- Double 当双击操作后Web元素的状态发生变化时使用单击操作。 Double 点击操作 Selenium Web 驱动程序可以使用预定义命令完成 Double 点击如下所述
Actions action = new Actions(driver); WebElement link = driver.findElement(By.ID ("Element ID")); action. doubleClick (link).perform();