如何使用Selenium WebDriver在Firefox 19中进行鼠标悬停操作?

17

我使用过Selenium 2.31。

我使用Actions类进行鼠标移动。使用它,我将鼠标移到菜单上,但其子菜单仅在Firefox旧版本中出现了一小段时间。

由于这个问题,我不能使用driver.findElement选择子菜单,因为它会抛出一个异常“元素无法滚动到视图中”。

这个问题有解决方案吗?


如果可能的话,您能否提供您正在测试的应用程序链接,以便我们可以调试问题? - HemChe
你用的是什么编程语言?Java、C# 还是其他的? - Ripon Al Wasim
6个回答

32

使用 actions 对象,首先移动菜单标题,然后移动到弹出式菜单项并单击它。不要忘记在最后调用 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()" 等同于 ".Hover()",这就是我需要的。 - FrankyHollywood
1
有没有办法让我的鼠标悬停在一个特定的时间?我已经可以悬停了,但子菜单很快就隐藏了,然后 Web Driver 就无法找到那个子菜单了。 - Helping Hands
1
@帮助之手 - 你可以使用ClickandHold()。 - Aishu

3

尝试这段代码... 这是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();

非常感谢。在第一个顶部菜单上使用act.MoveToElement(webElement).Perform()对于我来打开隐藏菜单非常关键。我之前漏掉了结尾的Perform(),只是MoveToElement(webElement)不够。 - GlobalCompe

3

另一种方法是使用Selenium的JavaScript Executor来强制显示元素的样式。

以下是一个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并使用selenium单击该元素。 您还可以级联查找主元素的子元素。

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

请注意,只有当您的悬停元素在悬停时更改显示样式时,才能实现此操作。

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

0

这个答案帮助我解决了我的问题。

我的挑战是找到菜单选项下面的链接。 在我悬停在菜单上之前,这个链接是看不见的。

对我来说至关重要的部分是发现除了悬停在菜单上之外,我还必须悬停在链接上才能与它交互。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接