Double Click in Selenium WebDriver

Double Click in Selenium WebDriver

Hello friends! in this post, we will learn to double click an element using Selenium Webdriver with Java. For double clicking an element in Selenium we make use of the Actions class. The Actions class provided by Selenium Webdriver is used to generate complex user gestures including right click, double click, drag and drop etc.

Code snippet to double click an element

Actions action = new Actions(driver);
WebElement element = driver.findElement(By.id("elementId"));
action.doubleClick(element).perform();

Here, we are instantiating an object of Actions class. After that, we pass the WebElement to be double clicked as parameter to the doubleClick() method present in the Actions class. Then, we call the perform() method to perform the generated action.

Sample code to double click an element

For the demonstration of the double click action, we will be launching Sample Site for Selenium Learning. Then we will double click the button on which the text “Double-click to generate alert box” is written. After that an alet box will appear, asserting that double-click action is successfully performed.

package seleniumTutorials;
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 {

public static void main(String[] args) throws InterruptedException{
WebDriver driver = new FirefoxDriver();

//Launching sample website
driver.get("#/sampleSiteForSelenium");
driver.manage().window().maximize();

//Double click the button to launch an alertbox
Actions action = new Actions(driver);
WebElement btn = driver.findElement(By.id("dblClkBtn"));
action.doubleClick(btn).perform();

//Thread.sleep just for user to notice the event
Thread.sleep(3000);

//Closing the driver instance
driver.quit();
}

}

That’s all we have in this post, share it will your friends. If you have any questions please comment below. Check out the complete tutorial here.

Selenium Complete Tutorial

От QA genius

Adblock
detector