How to double-click on a button in Selenium Webdriver?

Earlier, we discussed how to click on a button, and this post will focus on performing a double-click operation on the button in Selenium using Java.

We can double-click on a button using a couple of ways –

Let’s look at them one by one.

Using Actions class

Actions class provides a doubleClick() method that helps double clicking on a button.

We will perform the double-click operation on the below-highlighted element. You can find this element at https://testkru.com/Elements/Buttons.

double click highlighted element

We will first have to find the element, and then we can perform the double-click operation using the Actions class. So, let’s inspect the highlighted element to find the locator using which we can find it.

Inspect on the double click element

We can see that the element has a unique id (“doubleClick“). So, we can use it to find the element using the findElement() method.

WebElement webElement = driver.findElement(By.id("doubleClick"));

Now, we can use the Actions class to perform the double-click operation on the element.

Actions actions = new Actions(driver);
actions.doubleClick(webElement).perform();

Below is the whole program –

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.testng.annotations.Test;

public class CodekruTest {

	@Test
	public void test() {

		// pass the path of the chromedriver location in the second argument
		System.setProperty("webdriver.chrome.driver", "E:\\chromedriver.exe");
		WebDriver driver = new ChromeDriver();

		// opening the url
		driver.get("https://testkru.com/Elements/Buttons");

		WebElement webElement = driver.findElement(By.id("doubleClick")); // finding the button

		// double-clicking on the button
		Actions actions = new Actions(driver);
		actions.doubleClick(webElement).perform();

	}
}

Above code will perform the double-click operation.

Using JavascriptExecutor

Earlier, we used the doubleClick() method of the Actions class, but we can also double click on a button using simple Javascript code. Below javascript code can help us double click on a button.

var clickEvent  = document.createEvent ('MouseEvents');
clickEvent.initEvent ('dblclick', true, true);
arguments[0].dispatchEvent (clickEvent);

where arguments[0] is the web element we want to double click.

We can execute the above javascript code using the JavascriptExecutor in Selenium.

String javascriptCode = "var clickEvent  = document.createEvent ('MouseEvents');\r\n"
				+ "clickEvent.initEvent ('dblclick', true, true);\r\n"
				+ "arguments[0].dispatchEvent (clickEvent);";
		
JavascriptExecutor javascriptExecutor = (JavascriptExecutor)driver;
javascriptExecutor.executeScript(javascriptCode, webElement);

Here is the whole program, which can double click on a button using JavascriptExecutor.

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.testng.annotations.Test;

public class CodekruTest {

	@Test
	public void test() {

		// pass the path of the chromedriver location in the second argument
		System.setProperty("webdriver.chrome.driver", "E:\\chromedriver.exe");
		WebDriver driver = new ChromeDriver();

		// opening the url
		driver.get("https://testkru.com/Elements/Buttons");

		WebElement webElement = driver.findElement(By.id("doubleClick")); // finding the button

		String javascript = "var clickEvent  = document.createEvent ('MouseEvents');\r\n"
				+ "clickEvent.initEvent ('dblclick', true, true);\r\n" 
				+ "arguments[0].dispatchEvent (clickEvent);";

		// double click on the element
		JavascriptExecutor javascriptExecutor = (JavascriptExecutor) driver;
		javascriptExecutor.executeScript(javascript, webElement);

	}
}

This is it. We hope that you have liked the article. If you have any doubts or concerns, please feel free to write us in the comments or mail us at [email protected]

Related Articles
Liked the article? Share this on

Leave a Comment

Your email address will not be published.