3

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);
 clicker.contextClick(propButton).perform(); 
 return propButton;
}

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

olyv
5303 silver badges16 bronze badges
asked Sep 25, 2012 at 14:23
6
  • 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 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? 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 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? Commented Nov 16, 2012 at 6:36

3 Answers 3

2

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();
dzieciou
10.5k9 gold badges49 silver badges102 bronze badges
answered Nov 16, 2012 at 6:10
1
  • 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 Commented Mar 21, 2013 at 17:18
0

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);
actions.moveToElement(elem);
actions.contextClick(elem);
actions.perform();

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);
actions.moveToElement(elem);
actions.perform();
//then perform right click using ROBOT
 Robot robot = new Robot();
 robot.mousePress(InputEvent.BUTTON3_MASK);
 robot.mouseRelease(InputEvent.BUTTON3_MASK);
answered Feb 19, 2013 at 8:34
1
  • 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. Commented Jun 30, 2013 at 12:13
0

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);
clicker.ContextClick(testUserAccount).Perform();
// 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
showUserInfo.Click();
// 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
driver.SwitchTo().Window(popupHandle);

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.

answered Mar 21, 2013 at 17:23

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.