Right Click in Selenium:
- To perform some action, we may need to click on the right click action / references to an element.
- We use the action class in the Selenium WebDriver to work on mouse and keyboard actions to perform Right Click.
- Using Action class we create instance / object then call to contextClick Method to perform Right click operation on any WebPage.
Steps to Handle Right Click in Selenium Using Action Class:
- Create a Object of Action Class.
- findElement and Store into WebElement.
- Perform Right click operation using contextClick Method.
- Using contextClick Method we call perform().
contextClick Method in Action Class:
- Using contextClick Method we perform Right click operation on WebElement.
- contextClick Method perform important role in Action class when we want to perform Right Click operation.
- contextClick Method is time consuming as well as we don’t need to use KeyBoard Events to perform Right Click operation.
Right click on WebElement using Action Class contextClick Method:
1 2 3 4 5 6 7 8 | // Create object of Action class Actions action = new Actions(driver); // Find element using locator and store into WebElement WebElement element = driver.findElement(By.id("elementId")); // Perform Right click operation using action (object) on element. action.contextClick(element).perform(); |
Scenario’s To perform Right Click operation on WebElement using contextClick Method:
- setProperty of driver to Launch Browser.
- Create a driver instance of FirefoxDriver.
- Create object of Action class to perform Right Click Operation.
- Find element using locator and store into WebElement.
- Perform Right click operation using action (object) on element using contextClick method.
Example to Perform Right Click operation on WebElement using contextClick Method:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 | import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.firefox.FirefoxDriver; import org.openqa.selenium.interactions.Actions; public class Mouse_Hover { public static void main(String[] args) { System.setProperty("webdriver.gecko.driver","D:\\geckodriver.exe"); WebDriver driver = new FirefoxDriver(); driver.get("https://www.stqatools.com"); // Create object of Action class Actions action = new Actions(driver); // Find element using locator and store into WebElement WebElement element = driver.findElement(By.id("elementId")); // Perform Right click operation using action (object) on element using contextClick method. action.contextClick(element).perform(); } } |
Double click and Right Click in Selenium with Examples and How To Perform Right Click Action (Context Click) In Selenium and Select an Option from the Right-Click Menu in Selenium Webdriver and Perform Right Click Action Using WebDriver