How To Perform Right Click In Selenium WebDriver

In Selenium WebDriver to perform right click on any web element is not a complex process. All you have to do is create an instance of Action class and call the methods defined in it to perform the actions you want. Follow the below steps in order to perform right click on any web element :

1) Launch Eclipse, create a project and a package under it, and then a class named “tstclss.java”.

2) Write the code to navigate to URL “http://seleniumtutorialpoint.com/”.

3) There is an option “SAMPLE PAGE” available on the upper left side of the side. We have to right click on it and have to open it in a new tab.

The line of code you need to write in order to navigate to this site is :

WebDriver driver=new FirefoxDriver();
driver.get(“http://seleniumtutorialpoint.com/”); 

a

4) Now we have to perform right click on the option “Sample Page”. In order to do it, first we will create an instance of Action class and we will pass driver instance into it. But before creating the instance, we need to locate the web element “Sample Page” which is :

WebElement ele=driver.findElement(By.xpath(“//*[@id=’navigation’]/ul/li/a”));

The line of code which is to be added in the script for creation of Action class and performing methods is :

Actions act=new Actions(driver);
act.contextClick(ele).sendKeys(Keys.ARROW_DOWN).sendKeys(Keys.ENTER).build().perform();

b

Here the method sendKeys(Keys.ARROW_DOWN) is used to select an option from the list. If you will not add this method, the right click on the web element will be performed and the option list which appears after the right click will get disappeared without selecting any option.

We can perform the right click on any web element using the Robot class too. Here you first need to move the mouse to the particular web element and then perform the required action…!!!

Add Comment