In this tutorial, we will explain how to work with Actions Class for double clicking an element by taking a simple example. To do the same, we have created a Junit class.

We will exploit the capabilities of WebDriver’s Actions Class in order to perform Double Click. Check out the syntax below:

Actions action = new Actions(driver);

Script Creation

For script creation, create a Sample Java project in Eclipse IDE. Now create a Junit class and paste the code explained later in this blog.

Explanation of Application under Test

In order to explain the simulation of double click using WebDriver, we have designed a web page which includes two textboxes and a button. The scenario is where the user enters two unique strings values into these textboxes and double clicks on the “Cop Text” button. As soon as the user double clicks the button, a function is triggered which copies the text from Field1 into Field2.


Subsequent is the HTML code used to create the above mentioned webpage:

<!DOCTYPE html>

Field1: <input type="text" id="field1" value="Hello World!"><br>
Field2: <input type="text" id="field2"><br><br>

<button ondblclick="myFunction()">Copy Text</button>

<p>A function is triggered when the button is double-clicked. The function copies the text from Field1 into Field2.</p>

function myFunction() {
    document.getElementById("field2").value = document.getElementById("field1").value;


This code can also be found at


  • Launch the browser and open AUT
  • Enter a unique valid string in Field1
  • Enter a unique valid string in Field2
  • Double click on Copy Text button
  • Close the web browser

WebDriver code using Action Class

import static org.junit.Assert.*;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;
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 DoubleClick {

    // objects declaration
    WebDriver driver;

    public void setUp() throws Exception {
        // URL of the application under test
        String url = "file:///D:/Shruti/Protechskills/Blogs-Writeups/DoubleClick.html";
        // launch the firefox browser and open the application url
        driver = new FirefoxDriver();

    public void tearDown() throws Exception {
        // close the web browser window opened by WebDriver

    public void test() throws InterruptedException {
        String field1Text = "Double Click Test";
        String field2Text = "Text in field two";
        // enter a valid string in Field1

        WebElement field1 = driver.findElement("field1"));
        // enter a valid string in Field2
        WebElement field2 = driver.findElement("field2"));
        // double click the button
        WebElement button = driver.findElement(By.xpath("//button[contains(text(),'Copy Text')]"));
        Actions action = new Actions (driver);
        action.moveToElement(button).doubleClick().build().perform();// An alternative way could be - action.doubleClick(button).build().perform(); 

Take a note that Actions class is particularly used to emulate the most complex user interactions with the application. Users are advised to use Actions Class’ capabilities to perform mouse or keyboard actions.

Stay tuned…There is a lot more coming up in this space.

Share this:

3 thoughts on “Double Clicking an element in Selenium WebDriver

  1. ルイヴィトン – N級バッグ、財布 専門サイト問屋

    弊社は販売ルイ・ヴィトン) バッグ、財布、 小物、靴類などでございます。




    送料は無料です(日本全国)! ご注文を期待しています!


Leave a Reply

Your email address will not be published. Required fields are marked *