Double Click In Selenium

Double Click action is a frequently used by user. The most common use of double click happens in File Explorer.  Let’s consider on any Web Page, there might be some elements that require a double click to invoke an action on them. As earlier discussed we know that in Selenium there is no WebDriver API command which has the capability to double click on the WebElement.
Hence, the Action class method doubleClick(WebElement) is required to be used to perform this user action.

Manual Actions:

  • Launch the web browser and open the application
  • Find the required element and do double click on the element
  • Close the browser

Selenium WebDriver Steps

package automationTestingLab;

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","F:\\chromedriver.exe");
		WebDriver driver = new ChromeDriver();
		driver.manage().window().maximize();

		// Open the required URL
		driver.get("https://api.jquery.com/dblclick/");

		// As per the above URL we need to switch to frame. The targeted element is in the frame
		driver.switchTo().frame(0);
		
		// Create the object 'actions' of Actions class
		Actions action = new Actions(driver);
		
        //Find the targeted element
		WebElement doubleClickElement = driver.findElement(By.cssSelector("html>body>div"));
		
		// Here I used JavascriptExecutor interface to scroll down to the targeted element
		JavascriptExecutor js = (JavascriptExecutor) driver;
		js.executeScript("arguments[0].scrollIntoView();", doubleClickElement); // Scroll to the element
		
		// used doubleClick(element) method to do double click action
		action.doubleClick(doubleClickElement).build().perform();
		// Once clicked on the element, the color of element is changed to yellow color from blue color
		Thread.sleep(3000);
		driver.close();
	}
}

Next: Drag and Drop

Leave a comment

Design a site like this with WordPress.com
Get started