2013-03-11 76 views
16

我已經使用硒2.31。如何使用Firefox 19中的Selenium WebDriver進行鼠標懸停?

我已經使用動作類的鼠標移動。使用這個,我把鼠標移動到了菜單上,它的子菜單隻出現了幾分之一秒,與老版本的firefox不同。

由於此元素無法滾動到視圖中,因此無法使用driver.findElement選擇子菜單,因爲它引發異常。

有沒有解決方案?

+0

如果可能的話,你可以給你測試,使我們可以調試問題的應用程序的鏈接? – Hemanth 2013-03-11 17:46:42

+0

你用什麼語言? Java,C#還是什麼? – 2014-02-20 08:58:25

回答

32

對於動作對象,您應該先移動菜單標題,然後移動到彈出菜單項並單擊它。不要忘記在最後撥打actions.perform()。以下是一些示例Java代碼:

Actions actions = new Actions(driver); 
WebElement menuHoverLink = driver.findElement(By.linkText("Menu heading")); 
actions.moveToElement(menuHoverLink); 

WebElement subLink = driver.findElement(By.cssSelector("#headerMenu .subLink")); 
actions.moveToElement(subLink); 
actions.click(); 
actions.perform(); 
+1

這就是我所需要的:「.MoveToElement(menuElement).Perform()」equals「.Hover()」 – FrankyHollywood 2014-05-28 13:03:34

+1

有什麼辦法可以讓我在特定時間保持懸停?在我的情況下,我能夠懸停,但子菜單,然後隱藏很快,然後網絡驅動程序無法找到該子菜單。 – 2015-05-06 04:43:30

+1

@幫助手 - 你可以使用ClickandHold() – Aishu 2016-04-23 16:55:03

0

此答案幫助解決了我的問題。

我的挑戰是找到一個菜單選項下的鏈接。 直到我將鼠標懸停在菜單上,鏈接纔可見。

對我來說,這個關鍵的部分是發現,除了在菜單上懸停,我接下來必須懸停在鏈接上才能與之交互。

3

另一種解決方法是使用Selenium的JavaScript執行程序來強制顯示元素的樣式。

這方面的一個例子是沿着這條線在C#

//Use the Browser to change the display of the element to be shown 
(IJavaScriptExecutor)driver).ExecuteScript("document.getElementById('myId').stlye.display="block"); 

//navigate to your link that is now viewable 
driver.FindElement(By.Xpath('//LinkPath')).Click(); 

從那裏,你可以找到XPath來你的元素,並使用硒點擊的元素。您可以級聯此找到你的主要元素的兒童以及

//(IJavaScriptExecutor)ffbrowser).ExecuteScript("document.getElementById('myId').children[1].children[1].style.display='block'"); 

注意,這是唯一可能的,如果你有當鼠標懸停改變的顯示樣式懸停元素。

2

試試這個代碼... 它的升C代碼...

//Webelement is the main menu Link 
webElement = driver.FindElement(By.XPath("Your element xpath")); 
Actions act = new Actions(driver); 
     act.MoveToElement(webElement).Perform();//This opens menu list 

     System.Threading.Thread.Sleep(5000);//This line will help you to hold menu 
//This web element is the sub menu which is under main menu 
     webElement = driver.FindElement(By.XPath("Sub menu path")); 
     act.MoveToElement(webElement).Perform();//This opens menu list 
     System.Threading.Thread.Sleep(5000);//Holds menu 
    //This web element is the option you have to click 
     webElement = driver.FindElement(By.XPath("Path")); 
     webElement.Click(); 
+0

感謝您的。 act.MoveToElement(webElement)。執行();在第一個頂層菜單中,對於我的情況來說,打開隱藏的菜單至關重要。我最後錯過了Perform(),只是MoveToElement(webElement)還不夠。 – GlobalCompe 2016-04-25 16:15:10

1

如果你正在使用Ruby這會有所幫助。

1.首先你需要通過xpath或id找到元素。

2.然後使用方法action.move_to()。perform。

下面是代碼:

hover = WAIT.until{$driver.find_element(:xpath,"xpath")} 
    driver.action.move_to(hover).perform 
0
List<WebElement> list = driver.findElements(By.xpath("//a")); 
     for (int i=0;i<list.size();i++){ 
     if(list.get(i).getText().equalsIgnoreCase("cacique intimates M")) 
      { 
    new Actions(driver).moveToElement(list.get(i)).click().build().perform(); 
    System.out.println("Clicked on Parent Category"); 
    new Actions(driver).moveToElement(list.get(i)).moveToElement(driver.findElement(By.linkText("SPECIALTY BRAS"))).click().build().perform(); 
     break; 
    }       
    } 
+1

你想解釋它嗎?它做了什麼?它與現有的接受答案有什麼不同?順便說一下,「cacique intimates M」從哪裏來?在問題中找不到它。 – Rao 2016-06-03 22:14:30

相關問題