2017-06-21 53 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."); 
    } 

`

相關問題