Selenium 2.0 WebDriver和:hover伪类的解决方法

spa*_*bed 11 .net c# selenium nunit webdriver

有没有人可以提供如何通过涉及css伪类的已知Selenium问题的ac#示例:hover?

基本上,我正在为一个网站启动与selenium IDE(并在Visual Studio 2008中构建我的其余代码)进行回归测试,并且需要将鼠标悬停在div上,使其显示,然后单击div中的链接.

然而,我所有的努力都失败了,似乎许多人都有这个问题,没有解决方案.

提前致谢!

spa*_*bed 13

好的!所以我很感激帮助(我实际上看过那个帖子,但是.hover()类已被弃用了,我无法让它工作.但是,我确实找到了一个可靠的解决方法.

var phone = driver.FindElement(By.Id("phones"));
var phoneLi = phone.FindElements(By.TagName("li"));
Actions action  = new Actions(driver);//simply my webdriver
action.MoveToElement(phoneLi[1]).Perform();//move to list element that needs to be hovered
var click = action.MoveToElement(phoneLi[1].FindElements(By.TagName("a"))[0];//move to actual button link after the 'Li' was hovered
click.Click();
click.Perform(); //not too sure why I needed to use both of these, but I did. Don't care, it works ;)
IAlert alert = driver.SwitchTo().Alert();
alert.Accept();
Run Code Online (Sandbox Code Playgroud)

此外,您还需要使用一对使用语句.

using OpenQA.Selenium;
using OpenQA.Selenium.Interactions;
using OpenQA.Selenium.Interactions.Internal;
using OpenQA.Selenium.Support.UI;
Run Code Online (Sandbox Code Playgroud)

希望这可以帮助!

  • 非常感谢您提供"使用"声明.没有它,我一直很难在IE上使用W​​ebdriver(C#)搜索鼠标悬停的解决方案. (2认同)