Selenium right click scrolling down before selecting options

本小妞迷上赌 提交于 2019-12-25 07:12:21

问题


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();

}

回答1:


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.




回答2:


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();


    }


来源:https://stackoverflow.com/questions/37681416/selenium-right-click-scrolling-down-before-selecting-options

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!