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!!!
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.
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.
And one of the most commonly used methods of the class is contextClick(WebElement), which is used to perform the Right-Click action.
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 :)
*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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With