2

I'm trying to select an option from context menu, and sendKeys(Keys.ARROW_DOWN) is not working. All it does is that it moves the scroll of the page up and down(even though context menu is still open)

Actions action = new Actions(driver);
action.contextClick(element).build().perform();
action.sendKeys(Keys.ARROW_DOWN).sendKeys(Keys.Enter).build().perform();

the sendKeys method here just moves the page up and down without taking the opened context menu into consideration.(I also tried switching to an alert) Is there another way to select an option from the context menu?

Nour
  • 31
  • 1
  • 3
  • Possible duplicate of [Select an Option from the Right-Click Menu in Selenium Webdriver - Java](https://stackoverflow.com/questions/11428026/select-an-option-from-the-right-click-menu-in-selenium-webdriver-java) – Mansuro Apr 23 '18 at 12:25
  • http://www.seleniumeasy.com/selenium-tutorials/right-click-context-menu-webdriver-example – Grasshopper Apr 23 '18 at 13:02
  • YOu can use robot class in this case – Ankur Singh Apr 23 '18 at 13:47

2 Answers2

1

try this code :

action.contextClick(element).sendKeys(Keys.ARROW_DOWN).sendKeys(Keys.Enter).build().perform();
cruisepandey
  • 28,520
  • 6
  • 20
  • 38
  • 3
    Thank you for your suggestion, Tried it, the context menu does open, but the sendKeys(Keys.ARROW_DOWN) moves the scroll of the page down as if the context menu is not open. – Nour Apr 23 '18 at 12:42
  • then after context click , you have to find the element (whichever you wanna select) by any locators and click on it ! Please update your question with html of options you are getting after right click ! – cruisepandey Apr 23 '18 at 12:45
  • 3
    This context menu is not a part of html page, it is a menu coming from Chrome (a desktop application), so context menu options don't appear in the html. Manually after opening the context menu, pressing the up or down arrows then pressing enter on the keyboard works for selecting an option from the context menu. But when I run the code I wrote it just scrolls the page up and down(the focus is still on the page and not on the context menu itself). – Nour Apr 23 '18 at 13:15
  • Please share the html of 1. Element you are right clicking 2. What are the options available 3. Your desire output – cruisepandey Apr 23 '18 at 13:45
0

To navigate the context menu, you have to press the shift key AND arrow keys. Like this:

actionChains.context_click(element).send_keys(Keys.SHIFT, Keys.ARROW_DOWN, 'H')

The thing I have trouble with though is how to navigate down more than one menu item. Mine just always stops at the second item

Rabbid76
  • 202,892
  • 27
  • 131
  • 174