Select an Option from the Right-Click Menu in Selenium Webdriver - Java
We will take the help of WebDriver action class and perform Right Click. the below is the syntax :
Actions action = new Actions(driver).contextClick(element);
action.build().perform();
Below are the Steps we have followed in the example:
- Identify the element
- Wait for the presence of Element
- Now perform Context click
- After that we need to select the required link.
package com.pack.rightclick;
import org.openqa.selenium.Alert;
import org.openqa.selenium.By;
import org.openqa.selenium.NoSuchElementException;
import org.openqa.selenium.StaleElementReferenceException;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.interactions.Actions;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
import org.testng.Assert;
import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;
public class RightClickExample {
WebDriver driver;
String URL = "http://medialize.github.io/jQuery-contextMenu/demo.html";
@BeforeClass
public void Setup() {
driver = new FirefoxDriver();
driver.manage().window().maximize();
}
@Test
public void rightClickTest() {
driver.navigate().to(URL);
By locator = By.cssSelector(".context-menu-one.box");
WebDriverWait wait = new WebDriverWait(driver, 5);
wait.until(ExpectedConditions.presenceOfElementLocated(locator));
WebElement element=driver.findElement(locator);
rightClick(element);
WebElement elementEdit =driver.findElement(By.cssSelector(".context-menu-item.icon.icon-edit>span"));
elementEdit.click();
Alert alert=driver.switchTo().alert();
String textEdit = alert.getText();
Assert.assertEquals(textEdit, "clicked: edit", "Failed to click on Edit link");
}
public void rightClick(WebElement element) {
try {
Actions action = new Actions(driver).contextClick(element);
action.build().perform();
System.out.println("Sucessfully Right clicked on the element");
} catch (StaleElementReferenceException e) {
System.out.println("Element is not attached to the page document "
+ e.getStackTrace());
} catch (NoSuchElementException e) {
System.out.println("Element " + element + " was not found in DOM "
+ e.getStackTrace());
} catch (Exception e) {
System.out.println("Element " + element + " was not clickable "
+ e.getStackTrace());
}
}
@AfterClass
public void tearDown() {
driver.quit();
}
}
To select the item from the contextual menu, you have to just move your mouse positions with the use of Key down event like this:-
Actions action= new Actions(driver);
action.contextClick(productLink).sendKeys(Keys.ARROW_DOWN).sendKeys(Keys.ARROW_DOWN).sendKeys(Keys.RETURN).build().perform();
hope this will works for you. Have a great day :)
this is better approach and its successful :
Actions oAction = new Actions(driver);
oAction.moveToElement(Webelement);
oAction.contextClick(Webelement).build().perform(); /* this will perform right click */
WebElement elementOpen = driver.findElement(By.linkText("Open")); /*This will select menu after right click */
elementOpen.click();
*Using Robot class you can do this, Try following code:
Actions action = new Actions(driver);
action.contextClick(WebElement).build().perform();
Robot robot = new Robot();
robot.keyPress(KeyEvent.VK_DOWN);
robot.keyRelease(KeyEvent.VK_DOWN);
robot.keyPress(KeyEvent.VK_ENTER);
robot.keyRelease(KeyEvent.VK_ENTER);
[UPDATE]
CAUTION: Your Browser should always be in focus i.e. running in foreground while performing Robot Actions, other-wise any other application in foreground will receive the actions.