How to perform right click operation in selenium web driver using java

KeyPoints for the task

 Create an object of Actions class.

Actions action = new Actions(driver);

Call the method to perform an action.

action.contextClick(“Name of the element”).perform();

Task Scenario:

  •  Open browser.
  • Load the URL “http://www.seleniumeasy.com/test/”.
  • Right click on the logo of the home page.

Automate mouse events (Right click) in selenium web driver with an example.

 

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.interactions.Actions;

public class RightClick {
public static void main(String[] args) {
System.setProperty("webdriver.chrome.driver", "D:\\Automation\\chromedriver_win32\\chromedriver.exe");
// Open Browser
WebDriver driver = new ChromeDriver();
// Launching web site URL to automate.
driver.get("http://www.seleniumeasy.com/test/");
// Pass the driver object to the Action class.
Actions action = new Actions(driver);
// Find the element by id or xPath its up to you.
WebElement siteLogo = driver.findElement(By.id("site-name"));
// Pass the element to contextClick method and perform operation using object of
// action class.
action.contextClick(siteLogo).perform();
// Close browser.

// driver.close();

}