问题
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