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:
- Launch the web browser and open the application
- Find the required element and do double click on the element
- 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();
driver.get("http://api.jquery.com/dblclick/");
Actions act = new Actions(driver);
driver.switchTo().frame(0);
WebElement ele = driver.findElement(By.xpath("/html/body/div"));
// ((JavascriptExecutor) driver).executeScript("arguments[0].scrollIntoView();",
// ele);// scroll down for the element
String color = ele.getCssValue("color").toString();
System.out.println("color before double click:" + color); // blue
Thread.sleep(5000);
act.doubleClick(ele).build().perform();
Thread.sleep(2000);
color = ele.getCssValue("color").toString();
System.out.println("color after double click:" + color); // yellow
driver.close();
}
}