Double click Action in Selenium | Let’s move on to another action called double-click that can be performed using a mouse.
To perform double-click action on the current location, we will use doubleClick() method of actions class.
Basically, doubleClick() method comes into two flavors. First is, double-clicking a WebElement, which will learn in the next section and another is, double-clicking on the current location of cursor, which will be discussed here.
The syntax for double-click actions in Selenium is as follows:
public Actions DoubleClick()
This method does not take any input parameters and returns actions class instance.
Performing Double Click Action on Current Location
Let’s automate a scenario where we will double-click at the current location of the cursor.
Scenario to Automate:
1. Launch the Firefox web browser.
2. Open a URL “https://selenium08.blogspot.com/2019/11/double-click.html”.
3. Locate the element “Double-click” by using By.xpath.
4. Move the mouse cursor from its initial position to the current location.
5. Now, click on the current location (i.e. on Double-click element).
7. Close the web browser.
Let’s see the following source code related to the above scenario. Follow all the steps in the below source code.
Program source code 1:
package seleniumProject; import java.util.concurrent.TimeUnit; import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.firefox.FirefoxDriver; import org.openqa.selenium.interactions.Actions; public class DblClickAtCurrentLocation { public static void main(String[] args) { // Create a driver object for Firfox browser. WebDriver driver = new FirefoxDriver(); // Maximize web browser. driver.manage().window().maximize(); // Store URL in a variable URL of String type. String url = "https://selenium08.blogspot.com/2019/11/double-click.html"; // Call get() method and pass the URL as an input parameter. driver.get(url); // Wait for loading complete web page. driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS); // Locate WebElement by using By.xpath. WebElement dblclick = driver.findElement(By.xpath("//button[text() = 'Double-Click me to see Alert message']")); // Create an object of Actions class and pass the driver as an parameter. Actions actions = new Actions(driver); // Call moveToElement() method to move the mouse to the location of button element and pass element as an input parameter. actions.moveToElement(dblclick); // Line 1. // Call doubleClick() method to peform the double-click action at current location. actions.doubleClick(); // Line 2. // Call perform() method to perform a sequence of actions. actions.perform(); //Line 3. // The Line 1, Line 2, and Line 3 can also be written in a single line like this: // actions.moveToElement(dblclick).doubleClick().perform(); System.out.println("Double click action performed successfully at current location"); } }
Output: Double click action performed successfully at current location
In the above code, we have used moveToElement(WebElement element) method to move mouse cursor to the location of the button element and performed double-click action at current location.
The output has been shown in the below screenshot after performing double-click action on the double-click element on the demo web page.
Performing Double-click on WebElement
In the previous section, we have performed the double-click action on a current location moving from the initial position. Now, we will perform double-click action on an element using another overloaded version of doubleClick() method.
The API syntax of for double-click() method is as follows:
public Actions doubleClick( WebElement onElement)
This method accepts the target element as an input parameter that has to be performed double-clicked.
Here, we will use the same URL as you performed in the previous scenario. Let’s see the following source code for this.
Program source code 2:
package seleniumProject; import java.util.concurrent.TimeUnit; import org.openqa.selenium.Alert; import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.firefox.FirefoxDriver; import org.openqa.selenium.interactions.Actions; public class DblClickWebElement { public static void main(String[] args) { // Create a driver object for Firfox browser. WebDriver driver = new FirefoxDriver(); // Maximize web browser. driver.manage().window().maximize(); // Store URL in a variable URL of String type. String url="https://selenium08.blogspot.com/2019/11/double-click.html"; // Call get() method and pass the URL as an input parameter. driver.get(url); // Wait for loading complete web page. driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS); // Locate WebElement by using By.xpath. WebElement dblclick = driver.findElement(By.xpath("//button[text() = 'Double-Click me to see Alert message']")); // Create an object of Actions class and pass the driver as an parameter. Actions actions = new Actions(driver); // Call doubleClick() method to peform the double-click action at current location. actions.doubleClick(dblclick); // Line 2. // Call perform() method to perform a sequence of actions. actions.perform(); //Line 3. // The Line 1, Line 2, and Line 3 can also be written in a single line like this: // actions.doubleClick(dblclick).perform(); System.out.println("Double click action performed successfully on an element"); // Switching to Alert from web page. Alert alert = driver.switchTo().alert(); // Capture the text displayed on pop up and save it in a variable getText of type String. String getText = alert.getText(); System.out.println("Alert text: " +getText); // Click on OK button. alert.accept(); } }
Output: Double click action performed successfully on an element Alert text: You double clicked me.. Thank You..
Explanation:
1. In the preceding code, by creating an object of actions class, we have called doubleClick() method of actions class to perform a double-click on an element.
2. The doubleClick() method needs an element as an input parameter on which double-click will be fired. Therefore, we passed an element (dblclick) as an input parameter.
3. After executing the above code, you should see an alert message box saying that you double clicked me. Thank you.
4. We can also switch to an alert message box to capture message by using the Alert interface.
5. alert.getText() has been used to get the text from the alert message dialog box.
6. alert.accept() is used to press the OK button on the alert dialog box.
Recommended Tutorial:
Hope that this tutorial has covered almost all important points related to how to handle or perform double click action on an element in Selenium WebDriver. I hope that you will have understood this easy topic and performed practically.
Thanks for reading!!!
Next ⇒ How to right click on WebElement in Selenium⇐ PrevNext ⇒