Hi Guys. I have a problem with right click mouse event in Selenium 2.0. I'm using that method but in a console I've got:

Unable to locate element: {"method":"id","selector":"dijit_MenuItem_16_text"}

In my test case I've got

opPage.findElem("xpath element to I click on it").click();
opPage.rightClickOnAsset("xpath to element what is show after I right click mouse");

where the last method is defined as follows:

public WebElement rightClickOnAsset() {
  WebElement propButton = driver.findElement(By.xpath("xpath element to I click on it"));
  Actions clicker = new Actions(driver);

  return propButton;

Method is in another class in another Java file, look up the topic.

  • Are you able to find this element in any other part of the code? From the error message it looks like the code has problem finding the element and not specifically the right click. Commented Sep 25, 2012 at 16:29
  • Yes I'm able to find element and click left mouse button on it
    – Karol
    Commented Sep 26, 2012 at 12:28
  • After you left click does it change the state? If you get rid of the left click completely do you get the same error when you execute the rightClickOnAsset function?
    – Sam Woods
    Commented Sep 26, 2012 at 15:46
  • Has anyone had success with the contextClick action command? I've run into the same problem as @Karol where the context menu doesn't show, but left-clicking and finding the element is not a problem
    – squeemish
    Commented Sep 26, 2012 at 18:21
  • @Karol: That looks pretty strange, since you find element by XPath, why the error says about different selection method, by Id. Are you giving us correct code?
    – dzieciou
    Commented Nov 16, 2012 at 6:36

3 Answers 3


This worked for me to right click on a row in table.

//Selecting all cells of the HTML table
List<WebElement> elementNumList=driver.findElements(By.xpath("//a[@id='elemnetId']"));

//Taking 1st cell
WebElement link=elementNumList.get(0);

//Right Clicking
new Actions(driver).contextClick(link).perform();
  • I have a similar setup, although for me it's ContextCick, but this works great for my right menu options. I also use a popupHandle to deal with the new window, not sure if you need that
    – MichaelF
    Commented Mar 21, 2013 at 17:18

Method 1 (Using Webdriver Context Click)

// get web element
WebElement elem = By.id(selector);

//move mouse to element, then perform right click
Actions actions = new Actions(webdriver);

Method 2 (Using Java Robot to trigger right click event)

// get web element
 WebElement elem = By.id(selector);

//move mouse to element, 
Actions actions = new Actions(webdriver);

//then perform right click using ROBOT
    Robot robot = new Robot();
  • Guarav's example won't work as expected; the robot click occurs at the current PHYSICAL mouse location, not the one established by the actions, which the robot has no awareness of.
    – user5557
    Commented Jun 30, 2013 at 12:13

More detail on my comment above. I do use the ContextClick but because of the new window I have to up popupHandle to deal with the results of the right click:

// Get the existing handles
IList<string> existingHandles = this.driver.WindowHandles;
// This is an internal class that uses Context Click to perform the Right Click
Actions clicker = new Actions(driver);
// Now select the item in the Menu that displays all info for the User
IWebElement showUserInfo = driver.FindElement(By.Id("menu_ID"));
// The rest of this would be for the right click options
// Check for the launched window and save the new handle
string popupHandle = myConfig.findNewHandle(existingHandles, driver);
// Once the Window opens set focus on the order Window

Where findNewHandle is a separate function:

public string findNewHandle(IList<string> existingHandles, IWebDriver driver)
    var wait = new WebDriverWait(driver, TimeSpan.FromSeconds(20));

    // Check for the launched window and save the new handle
    string f = wait.Until<string>((d) =>
        d = driver;
        string foundHandle = null;
        IList<string> differentHandles = getDifference(existingHandles, d.WindowHandles);
        if (differentHandles.Count > 0)
            foundHandle = differentHandles[0];
        return foundHandle;
    return f;

For me, I couldn't do anything with the right click context until I was able to find and make active the window the right click brought up.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.