Double Click and Right Click in Selenium with Examples

In this tutorial, we will learn how to double click and right click in Selenium WebDriver with examples.

Selenium has emerged as a very powerful opensource that is widely being accepted across the world as the first choice of automation tool for automating web-based applications.

In this tutorial, I will explain to you how to perform mouse events like Right Click and Double Click in Selenium Webdriver. While automating an application sometimes we need to perform right-click or double-click on certain elements to accomplish certain tasks. We will use the actions class in Selenium to perform double click and right-click. If you want to know more about actions please refer to my post Actions Class in Selenium WebDriver.

How to Right Click in Selenium Using Action Class?

A right click operation is also called a Context Click. In order to perform a right click in Selenium, we will have to use contextClick() method that requires a WebElement as an argument. The ContextClick() emulates the behavior of doing a right click.

The syntax for doing right click in Selenium is as follows.

Actions actions = new Actions(driver);
WebElement element = driver.findElement(“By any locator”));
actions.contextClick(element).perform();

Example of Doing Right Click in Selenium WebDriver

To do a hands-on to verify right click please copy the following HTML code in notepad and save it with the file name rightclick.html.

When you will open the page and do a right click it will show a menu like the following screenshot.

Right click Action in Selenium

We will automate the following test scenario

  • Open the link
  • Do a right click on the page
  • Click on the second menu to open the Google search page.

How to Double Click in Selenium Using Action Class?

We can do double click using DoubleClick() of actions class.

Example of Doing Double Click in Selenium WebDriver

We can perform a double click using the following syntax.

Actions actions = new Actions(driver);
WebElement element = driver.findElement(“By any locator”));
actions.doubleClick(element).perform();

We will automate the following test scenario

How to do Right click in Selenium

Conclusion

We have learned how to do right click and double click using Actions Class in Selenium Webdriver.Hope you will find this article helpful.If you have any queries please do mention it in the comment box and don’t forget to share this article.

Recommended Posts

Leave a Reply