Hello friends, quite often during automation we need to right click or context click an element. Later, this action is followed up by pressing the UP/DOWN arrow keys and ENTER key to select the desired context menu element (check our tutorial on pressing the non-text keys in selenium - Pressing ARROW KEYS, FUNCTION KEYS and other non-text keys in Selenium).
For right 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.
Here, we are instantiating an object of Actions class. After that, we pass the WebElement to be right clicked as parameter to the contestClick() method present in the Actions class. Then, we call the perform() method to perform the generated action.
For the demonstration of the right click action, we will be launching Sample Site for Selenium Learning. Then we will right-click a textbox after which it's context menu will get displayed, asserting that right click action is successfully performed.