Double Click Action In Selenium WebDriver

In some scenarios, we may need to do double click action on a particular element to move further. In such cases, we use Actions class in Selenium WebDriver to work on Mouse and Keyboard Actions. Check out the below code for detailed explanation of Actions Class.

Scenario to be automated:
  1. Launch the web browser and open the application
  2. Find the required element and do double click on the element
  3. Close the browser to end the program
Copy the below mentioned script and work on this scenario.

import org.openqa.selenium.By;import org.openqa.selenium.JavascriptExecutor;import org.openqa.selenium.WebDriver;import org.openqa.selenium.WebElement;import org.openqa.selenium.chrome.ChromeDriver;import org.openqa.selenium.interactions.Actions;
public class DoubleClick {
public static void main(String[] args) throws InterruptedException { System.setProperty("webdriver.chrome.driver", "C://Drivers//chromedriver_win32//chromedriver.exe"); WebDriver driver = new ChromeDriver();
Actions act = new Actions(driver);
WebElement ele = driver.findElement(By.xpath("/html/body/div"));
((JavascriptExecutor) driver).executeScript("arguments[0].scrollIntoView();", ele);// scoll down for the element
String color = ele.getCssValue("color").toString(); System.out.println("color before double click:" + color); // blue
Thread.sleep(2000); act.doubleClick(ele).build().perform();
Thread.sleep(2000); color = ele.getCssValue("color").toString(); System.out.println("color after double click:" + color); // yellow


Post a Comment