- Learn Selenium
- Unmesh Gundecha Carl Cocchiaro
- 163字
- 2021-06-24 13:25:45
The double click on WebElement action
Now that we have seen a method that double-clicks at the current location, we will discuss another method that WebDriver provides to emulate the double-clicking of a WebElement.
The API syntax for the doubleClick() method is as follows:
public Actions doubleClick(WebElement onElement)
The input parameter for the preceding method is the target WebElement that has to be double-clicked, and the return type is the Actions class.
Let's see a code example for this. Open the DoubleClick.html file and single-click on the Click Me button. You shouldn't see anything happening. Now double-click on the button; you should see an alert saying Double Clicked !!. Now we will try to do the same thing using WebDriver. The following is the code to do that:
@Test
public void shouldDoubleClickElement() {
driver.get("http://guidebook.seleniumacademy.com/DoubleClick.html");
WebElement dblClick = driver.findElement(By.name("dblClick"));
Actions actions = new Actions(driver);
actions.doubleClick(dblClick).perform();
}
After executing the preceding code, you should see an alert dialog saying that the button has been double-clicked.