2012-06-14 71 views
0

我無法獲得我的下拉菜單的子菜單項,因爲子菜單僅在主菜單項的mouseOver事件中可見。Selenium.WebDriver!如何使用C#獲取下拉菜單的子菜單?

我的代碼:主菜單項目

mouseOver事件

 IWebElement element = WebDriver.FindElement(By.Id(elementID)); 

    Actions action = new Actions(WebDriver); 
    action.MoveToElement(element).Build().Perform();  

當我試圖讓我的菜單的子菜單項我得到以下錯誤:「...... OpenQA.Selenium.ElementNotVisibleException:無法單擊元素「。

你建議我做什麼?

回答

0

我做了一個谷歌搜索,發現一篇文章SELENIUM WEBDRIVER - 如何點擊一個隱藏的鏈接或菜單與解決我的問題。
Here's the article.

使用.mouseover()方法:

js.ExecuteScript("return $(\"a:contains('Fruits')\").mouseover();"); // Mouse hove to main menu 
webDriver.FindElement(By.LinkText("Banana")).Click(); 

,或者使用.hover()方法:

((JavascriptExecutor)driver).executeScript("$('div#Fruits').hover();"); 
    webDriver.FindElement(By.LinkText("Banana")).Click(); 

,或者使用.mouseenter()方法:

((JavascriptExecutor)driver).executeScript("$('div#Fruits').mouseenter();") 
webDriver.FindElement(By.LinkText("Banana")).Click();