2017-06-21 55 views
0

我想使用Selenium Webdriver在页面上右键单击并导航上下文菜单。该脚本应该打开右键菜单,并浏览最多2个选项,然后用回车键选择...Selenium Webdriver操作不能使用上下文菜单(右键单击)

driver.Navigate().GoToUrl("http://www.google.com"); 
//Google search bar 
IWebElement tb = driver.FindElement(By.Id("lst-ib")); 
Actions action = new Actions(driver); 
//Right Clicks outside of the search bar. 
action.MoveToElement(tb, -5, -5).ContextClick().Perform(); 
action.SendKeys(Keys.Up).SendKeys(Keys.Up).SendKeys(Keys.Return).Perform(); 

单击右侧的执行,因为它应该(在搜索栏之外),但是在此之后,没有证据表明向上箭头被按下,并且没有任何通过返回键选择。菜单选项应该在滚动时突出显示。

我使用的是最新版本的ChromeDriver 2.30,而Chrome 59.0.3071.109

+1

这在ChromeDriver一个已知的问题,我建议使用FirefoxDriver代替:https://bugs.chromium.org/p/chromedriver/issues/detail?id=1003 –

+0

谢谢你,我相信这是这样,因为我在完成其他操作时遇到了麻烦。使用修改键(ctrl + m)作为打开扩展的快捷方式比使用上下文菜单更容易,但这也不起作用...我想我会尝试使用Firefox并等待修复。 – dsidler

回答

0

如果你的应用程序只能在Windows上运行,你可以使用System.Windows.Forms.SendKey

action.MoveToElement(tb, -5, -5).ContextClick().Perform(); 
System.Windows.Forms.SendKeys.SendWait("{UP}"); 
System.Windows.Forms.SendKeys.SendWait("{UP}"); 
System.Windows.Forms.SendKeys.SendWait("{ENTER}"); 
0

如果是用户设计的上下文菜单。上下文菜单本身将具有定位

 `WebElement element =driver.findElement(By.xpath("your xpath")); 

     Actions action = new Actions(driver); 
     action.contextClick(selectedCell).build().perform(); 
     WebElement copyContext = driver.findElement(By.xpath("xpath of the right context column")); 

    if (copyContext .isEnabled()) 
    { 
     copyContext .click(); 
     log.info("Right context menu COPY CONTENT clicked."); 
    } 

`

相关问题