Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Select an Option from the Right-Click Menu in Selenium Webdriver - Java

I am using Selenium webdriver. I am not able to select (say 2nd) option from the Options opened on right click.

In my current code I am able to right click on webElement but could not select an Option from the list that is opened after right click, as it disappears automatically.

Actions action= new Actions(driver); action.contextClick(productLink).build().perform(); 

So with this code I am able to right click but the right click menu automatically disappears. I want to select say 2nd Option from Right click menu.

Please Help!!!

like image 495
Monika Yadav Avatar asked Jul 11 '12 07:07

Monika Yadav


People also ask

How do I select options from the right click menu in Selenium?

Selenium uses the Actions class to perform the right click action. The contextClick() is a method under Actions class to do the right click and once the menu opens, we can select an option from them via automation.

Which method of Selenium is used for right click?

We can perform right click on an element in Selenium with the help of Actions. In order to perform the right click action we will use contextClick () method. First we need to move to the specific element with the help of moveToElement() method then will do the right click with contextClick() method.

Which class is used for right click in Selenium WebDriver?

And one of the most commonly used methods of the class is contextClick(WebElement), which is used to perform the Right-Click action.


2 Answers

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 :)

like image 137
Neear Sharma Avatar answered Sep 21 '22 23:09

Neear Sharma


*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.

like image 22
Kushal Bhalaik Avatar answered Sep 19 '22 23:09

Kushal Bhalaik