0

I've seen this thread here and when I try to use sendKeys(Keys.ARROW_DOWN) instead of moving down the context menu the page scrolls down very quickly. How can I avoid this? I'm not sure how to wait for a context menu to appear, though I can wait for an element to be present.

Here's the function:

    public static void rightClickCopyImageLink(WebElement image){
    Actions copying = new Actions(driver);

    copying.contextClick(image).sendKeys(Keys.ARROW_DOWN).sendKeys(Keys.ARROW_DOWN).
    sendKeys(Keys.ARROW_DOWN).sendKeys(Keys.ARROW_DOWN).sendKeys(Keys.ARROW_DOWN).
    sendKeys(Keys.RETURN).build().perform();

}
Community
  • 1
  • 1
xandermonkey
  • 4,054
  • 2
  • 31
  • 53

2 Answers2

0

Try using the contextClick separately from other actions.

copying.contextClick(image).build().perform();
copying.sendKeys(Keys.ARROW_DOWN).sendKeys(Keys.ARROW_DOWN).
sendKeys(Keys.ARROW_DOWN).sendKeys(Keys.ARROW_DOWN).sendKeys(Keys.ARROW_DOWN).
sendKeys(Keys.RETURN).build().perform();

If you want you can add wait until the context menu appears after contextClick action performed.

  • Thanks for the reply, that was the first thing I tried. Does the same exact thing, however. – xandermonkey Jun 07 '16 at 14:44
  • I will add a wait and see if that works. Tips on the cleanest way to do so? Is there something other than time that I can make it wait for? – xandermonkey Jun 07 '16 at 14:45
  • Then the control is not on the context menu. Try manually on the application how it is behaving if you press down arrow after context click. If it is working as expected then you should wait until context menu is appears and get the control or some how make context menu get the control. – VaraPrasad Pakalapati Jun 07 '16 at 14:47
  • It depends Alex. If context menu got any properties like id, name, etc. you can get that object and wait until it is displayed. I don't prefer using Thread. But in worst case you can use it. But first try with Thread and see it is working. – VaraPrasad Pakalapati Jun 07 '16 at 14:50
  • If I do it manually it works fine, as long as I don't press the down arrow instantly after the right click button. – xandermonkey Jun 07 '16 at 14:50
  • Then it should work with actions as well. Because with actions you have to perform the same actions what you are doing manually – VaraPrasad Pakalapati Jun 07 '16 at 14:52
  • What am I doing wrong here, any idea? driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS); This line is between the contextClick call and the sendKeys call(s) – xandermonkey Jun 07 '16 at 15:07
  • I forced it to wait with Thread, at which point the down arrow still brought the page down.. Very odd. If I mouse right click and press the down arrow I immediately move down the contextmenu, NOT down the page. – xandermonkey Jun 07 '16 at 15:12
0

At first be sure that the image element is located correctly...

I assume on right click some submenus appear, do right click with seperate action chain. Then Wait for the element to appear.

Once it appears just move to element and press down arrow.

Skeleton code may be :

public static void rightClickCopyImageLink(WebElement image){
        Actions rightClick= new Actions(driver);
        rightClick.contextClick(image).build().perform();
        //Wait for submenu to appear waituntil(submenu to be located)
        //Move to located submenu <this is important>

        Actions MoveDownSubmenu = new Actions(driver);
    MoveToRightClickSubmenu.sendKeys(Keys.ARROW_DOWN).sendKeys(Keys.ARROW_DOWN).sendKeys(Keys.ARROW_DOWN).sendKeys(Keys.ARROW_DOWN).sendKeys(Keys.ARROW_DOWN).build().perform();


    }
theRoot
  • 571
  • 10
  • 33
  • I've tried your implementation with splitting between two Actions, doesn't work either. Thanks for the idea though. Any other thoughts? – xandermonkey Jun 07 '16 at 17:00
  • That's not the issue. I've got it tracked down now-it's a problem with the chrome webdriver....https://bugs.chromium.org/p/chromedriver/issues/detail?id=1003 – xandermonkey Jun 07 '16 at 17:19
  • If ur issue is still not resolved, try with AWT Robot key event it will definitely work but I would not recommend it since they are native events......use key press and release events once u moved to the submenu.. – theRoot Jun 07 '16 at 17:41
  • The problem is the chrome driver doesn't let you move to the submenu. It opens it but no matter what you do (including clicking, pressing escape, etc) you remain unable to move into the submenu. It's a chromedriver bug – xandermonkey Jun 07 '16 at 18:37
  • yes I understand what you are saying, but the event I pointed out is not a webdriver event , It is a native event (someting like generated by your OS) It has nothing to do with Selenium. It is like manual Keypress while web driver is running.https://docs.oracle.com/javase/7/docs/api/java/awt/event/MouseEvent.html – theRoot Jun 08 '16 at 06:42