2013-03-11 88 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